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

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc

MySQLoffersvariousstringdatatypes:1)CHARforfixed-lengthstrings,idealforconsistentlengthdatalikecountrycodes;2)VARCHARforvariable-lengthstrings,suitableforfieldslikenames;3)TEXTtypesforlargertext,goodforblogpostsbutcanimpactperformance;4)BINARYandVARB

TomasterMySQLBLOBs,followthesesteps:1)ChoosetheappropriateBLOBtype(TINYBLOB,BLOB,MEDIUMBLOB,LONGBLOB)basedondatasize.2)InsertdatausingLOAD_FILEforefficiency.3)Storefilereferencesinsteadoffilestoimproveperformance.4)UseDUMPFILEtoretrieveandsaveBLOBsco

BlobdatatypesinmysqlareusedforvoringLargebinarydatalikeImagesoraudio.1) Useblobtypes (tinyblobtolongblob) Basedondatasizeneeds. 2) Storeblobsin Perplate Petooptimize Performance.3) ConsidersxterNal Storage Forel Blob Romana DatabasesizerIndimprovebackupupe

ToadduserstoMySQLfromthecommandline,loginasroot,thenuseCREATEUSER'username'@'host'IDENTIFIEDBY'password';tocreateanewuser.GrantpermissionswithGRANTALLPRIVILEGESONdatabase.*TO'username'@'host';anduseFLUSHPRIVILEGES;toapplychanges.Alwaysusestrongpasswo

MySQLofferseightstringdatatypes:CHAR,VARCHAR,BINARY,VARBINARY,BLOB,TEXT,ENUM,andSET.1)CHARisfixed-length,idealforconsistentdatalikecountrycodes.2)VARCHARisvariable-length,efficientforvaryingdatalikenames.3)BINARYandVARBINARYstorebinarydata,similartoC


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

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
