Query speed based on order of columns

前端 未结 4 1741
半阙折子戏
半阙折子戏 2021-02-04 14:57

Does the order of the column types in your database have any affect on the query time?

For example, would a table with mixed ordering (INT, TEXT, VARCHAR, INT, TEXT) be

4条回答
  •  感动是毒
    2021-02-04 15:38

    I would suggest that there is absolutely no [significant] difference no matter how you order the columns.

    PostgreSQL: http://social.msdn.microsoft.com/Forums/en-US/sqldatabaseengine/thread/a7ce8a90-22fc-456d-9f56-4956c42a78b0

    SQL Server: http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/36713a82-315d-45ef-b74e-5f342e0f22fa

    I suspect the same for MySQL.

    All data is read in pages, so if your data fits into a single page it does not matter how you order the columns. If a disk block size is 2K, 4K, it will take in multiple to satisfy the "8K page request". If the disk block size is 64K (for large DB systems), you would already be buffering other data.

    Not only that, if a record is requested, it will normally retrieve all pages for the record, including the overflow to pages 2 and 3 if the data spans multiple pages. The columns are then worked out from the data retrieved. SQL Server has a limit on in-page data, which is about 8060 bytes. Anything larger is stored off the main data page, similar to TOAST for PostgreSQL and is not retrieved if the column is not used. It still does not matter where the column is in the order.

    In SQL Server for example, multiple bit fields are stored together in a bit patterned mask - this is irrespective of whether you put the columns next to each other. I would suspect MySQL and PostgreSQL to do much the same to optimize space.

    Note: [significant] - the only reason for this qualification is that, possibly, when extracting a particular column from a data page, having it in the beginning helps because the low-level assembly calls do not have to seek far in the memory block.

提交回复
热议问题