MySQL Performance – Does INT Primary Key Insert Order Affect Query Performance?

MySQLperformancequery-performance

Table t has an Int type primary key id without "Auto Increment"

CREATE TABLE `t` (
  `id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
);

Use the following three ways to insert data:

  1. Insert sequence id: 1-10000
  2. Descending insert id: 10000-1
  3. Random insert id: 1-10000

Does the order in which rows are inserted as suggested above affect performance in select, order by id and query operations?

Best Answer

I already answered a question like this

Of the three methods to insert data you mentioned, I would easily choose method #3 : Random insert id: 1-10000. Why ?

Ascending or Descending Ordered Insertion into an index structure will generate root and node splits 45% of the time as I mentioned in my two earlier posts.

In contrast, the ordering does not matter when it comes to reloading of a table from a mysqldump or upon the execution of a LOAD DATA INFILE command. That's because of the use of sorting mechanisms after the completion of the data load of the table and the index creation can be performed.

For example, look at how a MyISAM table is reloaded.

  • Data Pages for .MYD are populated
  • Index Pages for .MYI are populated with unique keys in parallel with Data Page Reload
  • Secondary Indexes are built by the internal execution of Repair by sorting

In that instance, order does not matter.

Switching gears to SELECT ORDER BY, performance could be nominally affected if needed keys from indexes are so split apart that multiple index pages must be accessed for ranges of data, this again leads me to choose method #3. The lower the frequency of index page splits, the fewer pages need to be read by an ORDER BY request.