Does the number of columns returned affect the speed of a query?

前端 未结 18 2015
既然无缘
既然无缘 2020-12-10 16:27

If I have two queries

SELECT Id, Forename, Surname
FROM Person
WHERE PersonName Like(‘%frank%’)

And

SELECT *
FROM Person
WH         


        
18条回答
  •  时光说笑
    2020-12-10 16:37

    SELECT * is usually never a good idea. It may not slow down your DBMS fetch a lot but it will probably result in more data being transmitted over the network than you need.

    However, that's likely to be swamped into insignificance by the use of the LIKE '%frank%' clause which is basically non-indexable and will result in a full table scan.

    You might want to consider cleaning up the data as it enters the database since that will almost certainly make subsequent queries run much faster.

    If you're after frank, then make sure it's stored as frank and use:

    select x,y,z from table where name = 'frank'
    

    If you want to get franklin as well, use:

    select x,y,z from table where name like 'frank%'
    

    Both of these will be able to use an index on the name column, "%frank%" will not.

提交回复
热议问题