Does the number of columns affect query performance?

前端 未结 6 680
春和景丽
春和景丽 2021-01-03 21:23

CASE 1: I have a table with 30 columns and I query using 4 columns in the where clause.

CASE 2: I have a table with 6 columns and I query using 4 columns in the whe

6条回答
  •  再見小時候
    2021-01-03 21:38

    Test it and see!

    There will be a performance difference, however 99% of the time you won't notice it - usually you won't even be able to detect it!

    You can't even guarantee that that the table with fewer columns will be quicker - if its bothering you then try it and see.

    Technical rubbish: (from the perspective of Microsoft SQL Server)

    With the assumption that in all other respects (indexes, row counts, the data contained in the 6 common columns etc...) the tables are identical, then the only real difference will be that the larger table is spread over more pages on disk / in memory.

    SQL server only attempts to read the data it absolutely requires, however it will always load an entire page at a time (8 KB). Even with the exact same amount data is required as the output to the query, if that data is spread over more pages then more IO is required.

    That said, SQL server is incredibly efficient with its data access, and so you are very unlikely to see a noticeable impact on performance except in extreme circumstances.

    Besides, it is also likely that your query will be run against the index rather than the table anyway, and so with indexes exactly the same size the change is likely to be 0.

提交回复
热议问题