MySQL不同存储引擎和不同分区字段对于查询的影响_MySQL
bitsCN.com
MySQL不同存储引擎和不同分区字段对于查询的影响
前提:每种表类型准备了200万条相同的数据。
表一 InnoDB & PARTITION BY RANGE (id)
Sql代码
CREATE TABLE `customer_innodb_id` (
`id` int(11) NOT NULL,
`email` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`phone` varchar(13) DEFAULT NULL,
`birth` date DEFAULT NULL,
`sex` int(1) DEFAULT NULL,
`avatar` blob,
`address` varchar(64) DEFAULT NULL,
`regtime` datetime DEFAULT NULL,
`lastip` varchar(15) DEFAULT NULL,
`modifytime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (100000) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (500000) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (1000000) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (1500000) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN (2000000) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;
查询结果:
Sql代码
mysql> select count(*) from customer_innodb_id where id > 50000 and id
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (1.19 sec)
mysql> select count(*) from customer_innodb_id where id > 50000 and id
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (0.28 sec)
mysql> select count(*) from customer_innodb_id where regtime > '1995-01-01 00:00
:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (4.74 sec)
mysql> select count(*) from customer_innodb_id where regtime > '1995-01-01 00:00
:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (5.28 sec)
表二 InnoDB & PARTITION BY RANGE (year)
Sql代码
CREATE TABLE `customer_innodb_year` (
`id` int(11) NOT NULL,
`email` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`phone` varchar(13) DEFAULT NULL,
`birth` date DEFAULT NULL,
`sex` int(1) DEFAULT NULL,
`avatar` blob,
`address` varchar(64) DEFAULT NULL,
`regtime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastip` varchar(15) DEFAULT NULL,
`modifytime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`regtime`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (YEAR(regtime ))
(PARTITION p0 VALUES LESS THAN (1996) ENGINE = InnoDB,
PARTITION p1 VALUES LESS THAN (1997) ENGINE = InnoDB,
PARTITION p2 VALUES LESS THAN (1998) ENGINE = InnoDB,
PARTITION p3 VALUES LESS THAN (1999) ENGINE = InnoDB,
PARTITION p4 VALUES LESS THAN (2000) ENGINE = InnoDB,
PARTITION p5 VALUES LESS THAN (2001) ENGINE = InnoDB,
PARTITION p6 VALUES LESS THAN (2002) ENGINE = InnoDB,
PARTITION p7 VALUES LESS THAN (2003) ENGINE = InnoDB,
PARTITION p8 VALUES LESS THAN (2004) ENGINE = InnoDB,
PARTITION p9 VALUES LESS THAN (2005) ENGINE = InnoDB,
PARTITION p10 VALUES LESS THAN (2006) ENGINE = InnoDB,
PARTITION p11 VALUES LESS THAN (2007) ENGINE = InnoDB,
PARTITION p12 VALUES LESS THAN (2008) ENGINE = InnoDB,
PARTITION p13 VALUES LESS THAN (2009) ENGINE = InnoDB,
PARTITION p14 VALUES LESS THAN (2010) ENGINE = InnoDB,
PARTITION p15 VALUES LESS THAN (2011) ENGINE = InnoDB,
PARTITION p16 VALUES LESS THAN (2012) ENGINE = InnoDB,
PARTITION p17 VALUES LESS THAN (2013) ENGINE = InnoDB,
PARTITION p18 VALUES LESS THAN (2014) ENGINE = InnoDB,
PARTITION p19 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */;
查询结果:
Sql代码
mysql> select count(*) from customer_innodb_year where id > 50000 and id
0;
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (5.31 sec)
mysql> select count(*) from customer_innodb_year where id > 50000 and id
0;
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (0.31 sec)
mysql> select count(*) from customer_innodb_year where regtime > '1995-01-01 00:
00:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (0.47 sec)
mysql> select count(*) from customer_innodb_year where regtime > '1995-01-01 00:
00:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (0.19 sec)
表三 MyISAM & PARTITION BY RANGE (id)
Sql代码
CREATE TABLE `customer_myisam_id` (
`id` int(11) NOT NULL,
`email` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`phone` varchar(13) DEFAULT NULL,
`birth` date DEFAULT NULL,
`sex` int(1) DEFAULT NULL,
`avatar` blob,
`address` varchar(64) DEFAULT NULL,
`regtime` datetime DEFAULT NULL,
`lastip` varchar(15) DEFAULT NULL,
`modifytime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (id)
(PARTITION p0 VALUES LESS THAN (100000) ENGINE = MyISAM,
PARTITION p1 VALUES LESS THAN (500000) ENGINE = MyISAM,
PARTITION p2 VALUES LESS THAN (1000000) ENGINE = MyISAM,
PARTITION p3 VALUES LESS THAN (1500000) ENGINE = MyISAM,
PARTITION p4 VALUES LESS THAN (2000000) ENGINE = MyISAM,
PARTITION p5 VALUES LESS THAN MAXVALUE ENGINE = MyISAM) */;
查询结果:
Sql代码
mysql> select count(*) from customer_myisam_id where id > 50000 and id
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (0.59 sec)
mysql> select count(*) from customer_myisam_id where id > 50000 and id
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (0.16 sec)
mysql> select count(*) from customer_myisam_id where regtime > '1995-01-01 00:00
:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (34.17 sec)
mysql> select count(*) from customer_myisam_id where regtime > '1995-01-01 00:00
:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (34.06 sec)
表四 MyISAM & PARTITION BY RANGE (year)
Sql代码
CREATE TABLE `customer_myisam_year` (
`id` int(11) NOT NULL,
`email` varchar(64) NOT NULL,
`name` varchar(32) NOT NULL,
`password` varchar(32) NOT NULL,
`phone` varchar(13) DEFAULT NULL,
`birth` date DEFAULT NULL,
`sex` int(1) DEFAULT NULL,
`avatar` blob,
`address` varchar(64) DEFAULT NULL,
`regtime` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`lastip` varchar(15) DEFAULT NULL,
`modifytime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`,`regtime`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8
/*!50100 PARTITION BY RANGE (YEAR(regtime ))
(PARTITION p0 VALUES LESS THAN (1996) ENGINE = MyISAM,
PARTITION p1 VALUES LESS THAN (1997) ENGINE = MyISAM,
PARTITION p2 VALUES LESS THAN (1998) ENGINE = MyISAM,
PARTITION p3 VALUES LESS THAN (1999) ENGINE = MyISAM,
PARTITION p4 VALUES LESS THAN (2000) ENGINE = MyISAM,
PARTITION p5 VALUES LESS THAN (2001) ENGINE = MyISAM,
PARTITION p6 VALUES LESS THAN (2002) ENGINE = MyISAM,
PARTITION p7 VALUES LESS THAN (2003) ENGINE = MyISAM,
PARTITION p8 VALUES LESS THAN (2004) ENGINE = MyISAM,
PARTITION p9 VALUES LESS THAN (2005) ENGINE = MyISAM,
PARTITION p10 VALUES LESS THAN (2006) ENGINE = MyISAM,
PARTITION p11 VALUES LESS THAN (2007) ENGINE = MyISAM,
PARTITION p12 VALUES LESS THAN (2008) ENGINE = MyISAM,
PARTITION p13 VALUES LESS THAN (2009) ENGINE = MyISAM,
PARTITION p14 VALUES LESS THAN (2010) ENGINE = MyISAM,
PARTITION p15 VALUES LESS THAN (2011) ENGINE = MyISAM,
PARTITION p16 VALUES LESS THAN (2012) ENGINE = MyISAM,
PARTITION p17 VALUES LESS THAN (2013) ENGINE = MyISAM,
PARTITION p18 VALUES LESS THAN (2014) ENGINE = MyISAM,
PARTITION p19 VALUES LESS THAN MAXVALUE ENGINE = MyISAM) */;
查询结果:
Sql代码
mysql> select count(*) from customer_myisam_year where id > 50000 and id
0;
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (2.08 sec)
mysql> select count(*) from customer_myisam_year where id > 50000 and id
0;
+----------+
| count(*) |
+----------+
| 449999 |
+----------+
1 row in set (0.17 sec)
mysql> select count(*) from customer_myisam_year where regtime > '1995-01-01 00:
00:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (0.56 sec)
mysql> select count(*) from customer_myisam_year where regtime > '1995-01-01 00:
00:00' and regtime
+----------+
| count(*) |
+----------+
| 199349 |
+----------+
1 row in set (0.13 sec)
结果汇总
序号 存储引擎 分区函数 查询条件 一次查询(sec) 二次查询(sec)
1 InnoDB id id 1.19 0.28
2 InnoDB id regtime 4.74 5.28
3 InnoDB year id 5.31 0.31
4 InnoDB year regtime 0.47 0.19
5 MyISAM id id 0.59 0.16
6 MyISAM id regtime 34.17 34.06
7 MyISAM year id 2.08 0.17
8 MyISAM year regtime 0.56 0.13
总结
1、对于按照时间区间来查询的,建议采用按照时间来分区,减少查询范围。
2、MyISAM性能总体占优,但是不支持事务处理、外键约束等。
bitsCN.com

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

When trying to open a disk image in VirtualBox, you may encounter an error indicating that the hard drive cannot be registered. This usually happens when the VM disk image file you are trying to open has the same UUID as another virtual disk image file. In this case, VirtualBox displays error code VBOX_E_OBJECT_NOT_FOUND(0x80bb0001). If you encounter this error, don’t worry, there are some solutions you can try. First, you can try using VirtualBox's command line tools to change the UUID of the disk image file, which will avoid conflicts. You can run the command `VBoxManageinternal

What happens when someone calls in airplane mode? Mobile phones have become one of the indispensable tools in people's lives. It is not only a communication tool, but also a collection of entertainment, learning, work and other functions. With the continuous upgrading and improvement of mobile phone functions, people are becoming more and more dependent on mobile phones. With the advent of airplane mode, people can use their phones more conveniently during flights. However, some people are worried about what impact other people's calls in airplane mode will have on the mobile phone or the user? This article will analyze and discuss from several aspects. first

On the Douyin platform, users can not only share their life moments, but also interact with other users. Sometimes the comment function may cause some unpleasant experiences, such as online violence, malicious comments, etc. So, how to turn off the comment function of TikTok? 1. How to turn off the comment function of Douyin? 1. Log in to Douyin APP and enter your personal homepage. 2. Click "I" in the lower right corner to enter the settings menu. 3. In the settings menu, find "Privacy Settings". 4. Click "Privacy Settings" to enter the privacy settings interface. 5. In the privacy settings interface, find "Comment Settings". 6. Click "Comment Settings" to enter the comment setting interface. 7. In the comment settings interface, find the "Close Comments" option. 8. Click the "Close Comments" option to confirm closing comments.

Java is a commonly used programming language used to develop various applications. However, just like other programming languages, Java has security vulnerabilities and risks. One of the common vulnerabilities is the file inclusion vulnerability (FileInclusionVulnerability). This article will explore the principle, impact and how to prevent this vulnerability. File inclusion vulnerabilities refer to the dynamic introduction or inclusion of other files in the program, but the introduced files are not fully verified and protected, thus

The impact of data scarcity on model training requires specific code examples. In the fields of machine learning and artificial intelligence, data is one of the core elements for training models. However, a problem we often face in reality is data scarcity. Data scarcity refers to the insufficient amount of training data or the lack of annotated data. In this case, it will have a certain impact on model training. The problem of data scarcity is mainly reflected in the following aspects: Overfitting: When the amount of training data is insufficient, the model is prone to overfitting. Overfitting refers to the model over-adapting to the training data.

Bad sectors on a hard disk refer to a physical failure of the hard disk, that is, the storage unit on the hard disk cannot read or write data normally. The impact of bad sectors on the hard drive is very significant, and it may lead to data loss, system crash, and reduced hard drive performance. This article will introduce in detail the impact of hard drive bad sectors and related solutions. First, bad sectors on the hard drive may lead to data loss. When a sector in a hard disk has bad sectors, the data on that sector cannot be read, causing the file to become corrupted or inaccessible. This situation is especially serious if important files are stored in the sector where the bad sectors are located.

What impact does chassis leakage have on computers? With the continuous advancement of technology, computers have gradually become an indispensable tool in people's lives. Whether it is work, study or entertainment, they are inseparable from the use of computers. However, while we enjoy the convenience brought by computers, we also need to pay attention to its security. Case leakage is a potential problem that, if not dealt with in time, may have a serious impact on the computer and the user. First of all, chassis leakage will cause damage to computer hardware. The computer's motherboard, power supply, internal circuits and other components are all inside the chassis. Once the chassis

Some users may consider buying mining cards for the sake of cheapness. After all, these cards are top-notch graphics cards. However, some gamers are worried about the impact of mining cards on playing games. Let’s take a look at the detailed introduction below. What are the effects of using a mining card to play games: 1. The stability of playing games with a mining card cannot be guaranteed, because the life of the mining card is very short and it is likely to become useless after just playing. 2. The mining card is basically a castrated version of the original version. Due to long-term wear and tear, the performance in all aspects may be weak. 3. In this way, users may not be able to display all the effects of the game when playing the game. 4. Moreover, the electronic components of the graphics card will age in advance, not to mention that playing games also consumes the graphics card, so it is drained to a greater extent, so the impact on the game is great. 5. In general, use mining cards to play games
