search
HomeDatabaseMysql TutorialSQL优化,百万级2张表关联,从40分钟到3秒的历程_MySQL

bitsCN.com

SQL优化,百万级2张表关联,从40分钟到3秒的历程

 

[sql] 

表结构如下:  

[sql] 

CREATE TABLE `deviceback` (  

  `id` int(11) NOT NULL AUTO_INCREMENT,  

  `imei` varchar(100) NOT NULL COMMENT '手机唯一标识',  

  `mid` varchar(50) DEFAULT NULL,  

  `mac` varchar(100) DEFAULT NULL,  

  `APNType` varchar(100) DEFAULT NULL,  

  `status` int(11) DEFAULT '0',  

  `ip` varchar(100) DEFAULT NULL,  

  `sn` varchar(100) DEFAULT NULL COMMENT '系列号',  

  `oem` varchar(100) DEFAULT NULL COMMENT '厂商',  

  `product` varchar(100) DEFAULT NULL COMMENT '产品',  

  `region` varchar(100) DEFAULT NULL COMMENT '区域',  

  `operator` varchar(100) DEFAULT NULL COMMENT '运营商',  

  `sim` varchar(100) DEFAULT NULL COMMENT 'sim卡号',  

  `push_time` timestamp NULL DEFAULT NULL COMMENT '第一次登陆时间',  

  `origin_version` varchar(100) DEFAULT NULL COMMENT '原始版本',  

  `province` varchar(100) DEFAULT NULL COMMENT '省份',  

  `provinceCode` varchar(100) DEFAULT NULL COMMENT '省份code',  

  `city` varchar(50) DEFAULT NULL COMMENT '城市',  

  `cityCode` varchar(50) DEFAULT NULL COMMENT '城市code',  

  `brands` varchar(50) DEFAULT '0',  

  `version` varchar(100) DEFAULT '0' COMMENT '客户端版本号',  

  `last_checktime` timestamp NULL DEFAULT NULL COMMENT '最后一次登录时间',  

  PRIMARY KEY (`id`),  

  FULLTEXT KEY `NewIndex1` (`imei`),  

  FULLTEXT KEY `NewIndex2` (`mid`),  

  FULLTEXT KEY `NewIndex3` (`product`),  

  FULLTEXT KEY `NewIndex4` (`brands`)  

) ENGINE=MyISAM AUTO_INCREMENT=6832460 DEFAULT CHARSET=utf8;  

[sql] 

CREATE TABLE `20130602_AppLog` (  

  `id` int(11) NOT NULL AUTO_INCREMENT,  

  `imei` varchar(100) DEFAULT NULL,  

  `mid` varchar(50) NOT NULL DEFAULT '',  

  `status` char(1) NOT NULL DEFAULT '0',  

  `mac` varchar(100) DEFAULT 'NULL',  

  `sn` varchar(100) DEFAULT NULL,  

  `sim` varchar(100) DEFAULT NULL,  

  `coperator` varchar(100) DEFAULT NULL,  

  `version` varchar(100) DEFAULT NULL,  

  `logintime` datetime DEFAULT NULL,  

  `ip` varchar(20) DEFAULT NULL,  

  `origin_version` varchar(100) DEFAULT NULL,  

  `now_version` varchar(100) DEFAULT NULL,  

  `APNType` varchar(20) DEFAULT NULL,  

  `oem` varchar(20) DEFAULT NULL,  

  `product` varchar(100) DEFAULT NULL,  

  `region` varchar(100) DEFAULT NULL,  

  `operator` varchar(100) DEFAULT NULL,  

  PRIMARY KEY (`id`),  

  FULLTEXT KEY `NewIndex1` (`imei`),  

  FULLTEXT KEY `NewIndex2` (`mid`)  

) ENGINE=MyISAM AUTO_INCREMENT=3123866 DEFAULT CHARSET=utf8;  

 

SQL如下:

 

 

[sql] 

SELECT *  

FROM   deviceback d,20130602_AppLog g  

WHERE d.mid=g.mid  

      AND d.imei=g.imei  

      AND d.mac=g.mac  

      AND d.brands=0  

      AND g.coperator '' limit 20;  

 

explain

结果:

 

SQL优化,百万级2张表关联,从40分钟到3秒的历程_MySQL

 

从这里可以看到ALL以及key为NULL,就是全表扫描,没有走索引,索引失效了!

 

1 然后我建议建上加上联合索引,试试看效果如何:

 

SQL优化,百万级2张表关联,从40分钟到3秒的历程_MySQL

 

2 效果还是比较慢的,看来得换种办法了,检查所有关联字段,将为null的数据改成''。还是没有效果。

 

3  不查*了,直接count(*)  看看结果集大小,结果还是卡住了,短时间内没有出查询结果。

 

4,到这里我猜测估计是数据的构成问题,那就一个个条件去掉去尝试了。

SELECT *

FROM   deviceback d,20130602_AppLog g

WHERE d.mid=g.mid

      AND d.imei=g.imei

      AND d.mac=g.mac

      AND d.brands=0 limit 20;

去掉 条件试试看,God,结果是37秒就出来了,好,大概问题找到了,出在这条判断语句里面,也就是

AND g.coperator '' 这个影响还蛮大的。

 

那就先在20130602_AppLog 表的coperator字段单独建索引试试看,然后查下 20130602_AppLog里面 g.coperator ''的有多少?

结果是比较令人欣慰的,单独查询,一秒不到出来结果了,6W多条纪录。

 SELECT COUNT(1) 

       FROM 20130602_AppLog gg

       WHERE gg.coperator ''

63987

 

然后再试试整个sql,看需要多长时间。

先看下explain结果:

 

SQL优化,百万级2张表关联,从40分钟到3秒的历程_MySQL

 

SELECT *

FROM   deviceback d,20130602_AppLog g

WHERE d.mid=g.mid

      AND d.imei=g.imei

      AND d.mac=g.mac

      AND d.brands=0

      AND g.coperator '' limit 20;

Great,不到3秒就出来结果了。

 

5,limit 20是OK了,我还想试试不限制limit的话,要多久可以查询出来结果。

直接执行

SELECT *

FROM   deviceback d,20130602_AppLog g

WHERE d.mid=g.mid

      AND d.imei=g.imei

      AND d.mac=g.mac

      AND d.brands=0

      AND g.coperator '' ;

卡住了,很久都没有出来结果,在想是否是数据的问题?切换下where条件后面的字段顺序试试看。

 

SELECT d.mid,g.mac,

FROM   deviceback d,20130602_AppLog g

WHERE 

       d.imei=g.imei

      AND d.mac=g.mac

      AND d.mid=g.mid

      AND d.brands=0

      AND g.coperator '' ;

 OK,27秒出来了,AND d.mid=g.mid这个之前在第一条,现在放在后面,原因是手机唯一标示这个字段有空值。

God,最讨厌关键业务字段null值了,给我们的优化工作带来巨大的烦恼。

 

切记:大家以后千万要记住关键业务字段不能允许录入null值。

bitsCN.com
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQL: String Data Types and ENUMs?MySQL: String Data Types and ENUMs?May 13, 2025 am 12:05 AM

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

MySQL BLOB: how to optimize BLOBs requestsMySQL BLOB: how to optimize BLOBs requestsMay 13, 2025 am 12:03 AM

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Adding Users to MySQL: The Complete TutorialAdding Users to MySQL: The Complete TutorialMay 12, 2025 am 12:14 AM

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

Mastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMastering MySQL String Data Types: VARCHAR vs. TEXT vs. CHARMay 12, 2025 am 12:12 AM

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

MySQL: String Data Types and Indexing: Best PracticesMySQL: String Data Types and Indexing: Best PracticesMay 12, 2025 am 12:11 AM

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

mPDF

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),