Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why mysql partitioning has no effect in my case

I try to test benefits of partitioning in Mysql

I created two tables: one partitioned other not.

Every table has 10M of records in it.

I want fast querying by "user_to_id" column.

Partitioned table (1024 parts):

CREATE TABLE `neworder10M_part_byuser` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `site_from_id` int(11) NOT NULL,
  `site_to_id` int(11) NOT NULL,
  `user_from_id` int(11) NOT NULL,
  `user_to_id` int(11) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`id`,`user_to_id`),
  KEY `composite_cover` (`user_to_id`,`user_from_id`,`site_from_id`,`site_to_id`,`created`)
) ENGINE=InnoDB 
/*!50100 PARTITION BY HASH (user_to_id)
PARTITIONS 1024 */ |

Table with clustered key (not partitioned):

CREATE TABLE `neworder_10M` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `site_from_id` int(11) NOT NULL,
  `site_to_id` int(11) NOT NULL,
  `user_from_id` int(11) NOT NULL,
  `user_to_id` int(11) NOT NULL,
  `created` datetime NOT NULL,
  PRIMARY KEY (`user_to_id`,`id`),
  UNIQUE KEY `id_UQ` (`id`)
) ENGINE=InnoDB;

when i benchmark both tables with python script for 1000 reqs:

for i in xrange(1,REQS):
    user_id = random.randint(1,10000);
    cursor.execute("select * from neworder10M_part_byuser where user_to_id=%s;" % (user_id))

Partitioned table: 22 rps Not partitioned: 22.7 rps

Why are there no speed benefits with the partitioned table? As I expect smaller data - faster querying.

And explain also shows that partition used:

mysql> explain select * from neworder10M_part_byuser where user_to_id=6867;
+----+-------------+-------------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------------+
| id | select_type | table                   | partitions | type | possible_keys   | key             | key_len | ref   | rows | filtered | Extra       |
+----+-------------+-------------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | neworder10M_part_byuser | p723       | ref  | composite_cover | composite_cover | 4       | const | 1009 |   100.00 | Using index |
+----+-------------+-------------------------+------------+------+-----------------+-----------------+---------+-------+------+----------+-------------+

but I did not see real speed improve in reality.... what I am doing wrong ?

Tables fill code:

def send_orders(cur,users=10000,orders=10000000):
    for i in xrange(1,orders+1): //10000000 rows here
        print i
        from_user = random.randint(1,users)
        to_user = random.randint(1,users)
        from_site = random.randint(1,10000)
        to_site = random.randint(1,10000)
        cur.execute("INSERT INTO neworder (site_from_id, site_to_id,user_from_id, user_to_id,created)  VALUES ('%d','%d','%d','%d',NOW());" % (from_user,to_user,from_site,to_site))

version of mysql: Ver 14.14 Distrib 5.7.12, for Linux (x86_64). Hard drive is ssd.

like image 240
Evg Avatar asked Oct 03 '26 08:10

Evg


1 Answers

(This is both an answer to the question and a rebuttal to some of the comments.)

If your WHERE clause can cause partition pruning to happen, then it can help make a composite index efficient. Hence, no advantage over non-partitioned table, given that you can choose a better index.

Think of partition pruning as shrinking the BTree depth by 1 level. But then you have to do the pruning. Result: virtually the same cost. This is my answer to the question of "range scan on 10M rows unpartitioned vs 10K rows in one partition". (@spencer7593's answers are good, too.)

There are only 4 use cases that I have found where PARTITIONing improves performance. There are in my blog.

BY RANGE is the only useful partitioning method. BY HASH, which you are using, seems to be totally useless. In particular, if you do a range scan on the 'partition key', it will necessarily scan all partitions -- no 'pruning' is possible.

It is usually inefficient to put the partition key first in any key.

UNIQUE KEY id_UQ (id) -- make that a plain INDEX for your non-partition test; it will be more efficient. And it will be sufficient for handling AUTO_INCREMENT.

(Oops, @spencer7593 already said some of these things, and pointed to my blog. Thanks. I wrote it because I get tired of repeating myself on the forums.)

Your particular query (SELECT ... WHERE user_to_id = constant) is a good way to demonstrate how useless PARTITIONing (of any kind) is. Is that your real query? You may actually have some other queries that could benefit from partitioning; let's see them.

"50x faster on smaller table" -- caching? Does the smaller table fit in the buffer_pool, but the bigger one not? I/O is the biggest factor in performance.

If WHERE user_to_id = constant is always in your queries, then have user_to_id as the first column in each index (except INDEX(id)) in a non-partitioned table. Think of that as equivalent to 'pruning'.

like image 113
Rick James Avatar answered Oct 05 '26 21:10

Rick James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!