select * vs select column

前端 未结 12 983
后悔当初
后悔当初 2020-11-22 05:42

If I just need 2/3 columns and I query SELECT * instead of providing those columns in select query, is there any performance degradation regarding more/less I/O

12条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 06:00

    The accepted answer here is wrong. I came across this when another question was closed as a duplicate of this (while I was still writing my answer - grr - hence the SQL below references the other question).

    You should always use SELECT attribute, attribute.... NOT SELECT *

    It's primarily for performance issues.

    SELECT name FROM users WHERE name='John';

    Is not a very useful example. Consider instead:

    SELECT telephone FROM users WHERE name='John';
    

    If there's an index on (name, telephone) then the query can be resolved without having to look up the relevant values from the table - there is a covering index.

    Further, suppose the table has a BLOB containing a picture of the user, and an uploaded CV, and a spreadsheet... using SELECT * will willpull all this information back into the DBMS buffers (forcing out other useful information from the cache). Then it will all be sent to client using up time on the network and memory on the client for data which is redundant.

    It can also cause functional issues if the client retrieves the data as an enumerated array (such as PHP's mysql_fetch_array($x, MYSQL_NUM)). Maybe when the code was written 'telephone' was the third column to be returned by SELECT *, but then someone comes along and decides to add an email address to the table, positioned before 'telephone'. The desired field is now shifted to the 4th column.

提交回复
热议问题