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:
- Insert sequence
id
: 1-10000 - Descending insert
id
: 10000-1 - 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
Oct 26, 2012
: How badly does innodb fragment in the face of somewhat out-of-order insertions?Jun 28, 2012
: Benefits of BTREE in MySQLOf 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 aLOAD 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.
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 choosemethod #3
. The lower the frequency of index page splits, the fewer pages need to be read by anORDER BY
request.