哪一个是最快的:“*”或显式列名? [关闭]

I need to find whether using * or select only the fields you want is faster. In my case, there will be only one match ever.

So, which is faster? This :

SELECT id, name,description,address,phone,img FROM companies WHERE username = '$cname' LIMIT 1

or this one?

SELECT * FROM companies WHERE username = '$cname' LIMIT 1

Do you have any suggestions?

tl;dr It Just Doesn't Matter.

The only case where it would make any difference is if there was lots of extra data pulled in. The extra overhead needed to be "detectable" over all the other network/disk/processor factors will vary: "Try It And See", or, run a performance analysis. Also, even if there were additional columns, since it was limited to one record, it is prudent to remember: work = effort * quantity.

I prefer the former explicit syntax myself, except for one-off interactive queries. SQL was meant to be used in a well-shaped manner.

Of course, if the columns in * matched the covered columns, then there should be absolutely no difference in terms of performance as they will have the same "shape" and query plans.

Happy coding.

As a general rule, retrieving only the data you need will be faster. I don't believe there's a case where SELECT * will be faster for this query. (It's considered bad form anyway.) In many cases, the difference will be miniscule, particularly since you're only retrieving a single row. However, if your table has, say, a BLOB column that isn't one of the named columns, or if it has 50 columns, then the difference might be significant.