一、表结构DDL

  1. root@dbmonitor-qb-01 10:59: [trswcmtest]> show create table WCMMetaTableWebSite\G
  2. *************************** 1. row ***************************
  3. Table: WCMMetaTableWebSite
  4. Create Table: CREATE TABLE `wcmmetatablewebsite` (
  5. `WCMMetaTableWebSiteID` int(11) NOT NULL DEFAULT '0',
  6. `MetaDataId` int(11) DEFAULT NULL,
  7. `ChannelId` int(11) DEFAULT NULL,
  8. `CrUser` varchar(50) DEFAULT NULL,
  9. `CrTime` timestamp NULL DEFAULT NULL,
  10. `FromType` int(11) DEFAULT NULL,
  11. `fromid` varchar(50) DEFAULT NULL,
  12. `ShareDocId` int(11) DEFAULT NULL,
  13. `Title` varchar(750) DEFAULT NULL,
  14. `OriginalTitle` varchar(750) DEFAULT NULL,
  15. `OriginalAddress` varchar(750) DEFAULT NULL,
  16. `Channel` varchar(750) DEFAULT NULL,
  17. `HomeTitle` varchar(750) DEFAULT NULL,
  18. `Author` varchar(75) DEFAULT NULL,
  19. `MoveTitle` varchar(750) DEFAULT NULL,
  20. `Keywords` varchar(750) DEFAULT NULL,
  21. `Abstract` varchar(1125) DEFAULT NULL,
  22. `ReadingPics` varchar(750) DEFAULT NULL,
  23. `AudioVideo` varchar(750) DEFAULT NULL,
  24. `IssueTime` timestamp NULL DEFAULT NULL,
  25. `CommentSettings` int(11) DEFAULT NULL,
  26. `FlowVersionTime` timestamp NULL DEFAULT NULL,
  27. `OriginMetaDataId` int(11) DEFAULT NULL,
  28. `DocType` int(11) DEFAULT NULL,
  29. `METALOGO` varchar(300) DEFAULT NULL,
  30. `METALOGOURL` varchar(4000) DEFAULT NULL,
  31. `Content` longtext,
  32. `AdditionalTextTop` longtext,
  33. `AdditionalTextBottom` longtext,
  34. `AdditionalTextLeft` longtext,
  35. `AdditionalTextRight` longtext,
  36. `Hits` int(11) DEFAULT NULL,
  37. `AttributionLabel` varchar(750) DEFAULT NULL,
  38. `OutLinePics` varchar(750) DEFAULT NULL,
  39. `Editor` varchar(750) DEFAULT NULL,
  40. `EditorTrueName` varchar(750) DEFAULT NULL,
  41. `TitleColor` varchar(300) DEFAULT NULL,
  42. `DocSource` int(11) DEFAULT NULL,
  43. `SrcMetaDataId` int(11) DEFAULT NULL,
  44. `DocWordsCount` int(11) DEFAULT NULL,
  45. `attachpic` int(11) DEFAULT NULL,
  46. `attachvideo` int(11) DEFAULT NULL,
  47. `attachaudio` int(11) DEFAULT NULL,
  48. `FgdError` int(2) DEFAULT NULL,
  49. `Remarks` varchar(750) DEFAULT NULL,
  50. `CrDept` varchar(1500) DEFAULT NULL,
  51. `HTMLContent` longtext,
  52. `srcUrl` varchar(300) DEFAULT NULL,
  53. `UpdateCount` int(11) DEFAULT NULL,
  54. `original` int(11) DEFAULT '0',
  55. `IsPushHome` int(11) DEFAULT '0',
  56. `DocSourceName` varchar(300) DEFAULT NULL,
  57. `TimingPublishTime` timestamp NULL DEFAULT NULL,
  58. `LeadTitle` varchar(750) DEFAULT NULL,
  59. `SubTitle` varchar(750) DEFAULT NULL,
  60. `specialFile` varchar(750) DEFAULT NULL,
  61. `specialCatalog` varchar(300) DEFAULT NULL,
  62. `specialContent` longtext,
  63. `mlfrootid` int(11) DEFAULT '0',
  64. `ISCOMMENTED` int(2) DEFAULT NULL,
  65. `customtime` timestamp NULL DEFAULT NULL,
  66. `AudioVideoUrl` varchar(600) DEFAULT NULL,
  67. `isContainSensitiveWords` int(11) DEFAULT NULL,
  68. `ORIGINALTYPE` int(11) DEFAULT NULL,
  69. `ORIGINALCHILDTYPE` int(11) DEFAULT NULL,
  70. `IsSecondReview` int(11) DEFAULT NULL,
  71. PRIMARY KEY (`WCMMetaTableWebSiteID`),
  72. UNIQUE KEY `IX_WebSite_282457558` (`MetaDataId`) USING BTREE
  73. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  74. 1 row in set (0.01 sec)
  75. root@dbmonitor-qb-01 09:45: [trswcmtest]> show create table WCMChnlDoc\G
  76. *************************** 1. row ***************************
  77. Table: WCMChnlDoc
  78. Create Table: CREATE TABLE `wcmchnldoc` (
  79. `CHNLID` int(11) NOT NULL,
  80. `DOCID` int(11) NOT NULL,
  81. `DOCORDER` int(11) NOT NULL DEFAULT '0',
  82. `DOCSTATUS` int(11) NOT NULL DEFAULT '0',
  83. `CRUSER` varchar(100) NOT NULL DEFAULT 'admin',
  84. `CRTIME` datetime DEFAULT NULL,
  85. `DOCPUBTIME` datetime DEFAULT NULL,
  86. `DOCPUBURL` varchar(300) DEFAULT NULL,
  87. `RECID` int(11) NOT NULL,
  88. `DOCORDERPRI` int(11) NOT NULL DEFAULT '0',
  89. `INVALIDTIME` datetime DEFAULT NULL,
  90. `OPERUSER` varchar(50) DEFAULT NULL,
  91. `OPERTIME` datetime DEFAULT NULL,
  92. `MODAL` int(11) DEFAULT '1',
  93. `DOCRELTIME` datetime DEFAULT NULL,
  94. `DOCCHANNEL` int(11) DEFAULT NULL,
  95. `DOCFLAG` int(11) DEFAULT NULL,
  96. `DOCKIND` int(11) DEFAULT '0',
  97. `SITEID` int(11) NOT NULL DEFAULT '0',
  98. `SRCSITEID` int(11) NOT NULL DEFAULT '0',
  99. `DOCFIRSTPUBTIME` datetime DEFAULT NULL,
  100. `NODEID` int(11) DEFAULT '0',
  101. `CRDEPT` varchar(200) DEFAULT NULL,
  102. `DOCOUTUPID` int(11) DEFAULT '0',
  103. `DOCFORM` int(11) DEFAULT '0',
  104. `DOCLEVEL` int(11) DEFAULT NULL,
  105. `attachpic` smallint(6) DEFAULT NULL,
  106. `POSCHNLID` int(11) DEFAULT '0',
  107. `ISPUSHTOPCHNL` int(2) DEFAULT '0',
  108. `HIDDEN` int(2) DEFAULT '0',
  109. `DOCTYPE` int(2) DEFAULT '0',
  110. `ISTIMINGPUBLISH` int(2) DEFAULT '0',
  111. `GDORDER` int(2) DEFAULT NULL,
  112. `setTopInfo` varchar(100) DEFAULT NULL,
  113. `OriginDocId` int(9) DEFAULT NULL,
  114. `ATTACHVIDEO` int(2) DEFAULT '0',
  115. `ATTACHAUDIO` int(2) DEFAULT '0',
  116. `SrcMetaDataId` int(11) DEFAULT NULL,
  117. `pubUser` varchar(50) DEFAULT NULL,
  118. `mrsFlag` int(2) DEFAULT NULL,
  119. `timingPubUser` varchar(50) DEFAULT NULL,
  120. `isTransmit` int(2) DEFAULT NULL,
  121. `TIMINGPUBUSERDEPT` varchar(200) DEFAULT NULL,
  122. `PUBUSERDEPT` varchar(200) DEFAULT NULL,
  123. `DocOldStatus` int(11) DEFAULT '0',
  124. `isZhengShen` int(2) DEFAULT '0',
  125. `ClientExamine` int(2) DEFAULT '0',
  126. `srcChannelId` int(11) DEFAULT NULL,
  127. PRIMARY KEY (`RECID`),
  128. KEY `IX_WCMCHNLDOC_CHNL_DOC` (`CHNLID`,`DOCID`) USING BTREE,
  129. KEY `IX_WCMCHNLDOC_CHNL_ORDER` (`CHNLID`,`DOCORDER`) USING BTREE,
  130. KEY `IX_WCMCHNLDOC_DOCID_MODAL` (`DOCID`,`MODAL`) USING BTREE,
  131. KEY `idx_DocId_SrcMetaDataId` (`DOCID`,`SrcMetaDataId`),
  132. KEY `idx_chnlid_docstatus_dockind_docpubtime` (`CHNLID`,`DOCSTATUS`,`DOCKIND`,`DOCPUBTIME`)
  133. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

二、优化前SQL

  1. root@dbmonitor-qb-01 10:57: [trswcmtest]> desc select WCMMetaTableWebSite.MetaDataId from WCMMetaTableWebSite where exists(select WCMChnlDoc.DocId from WCMChnlDoc where WCMChnlDoc.ChnlId>=1 and WCMChnlDoc.Modal>=1 and WCMChnlDoc.DocId=WCMMetaTableWebSite.MetaDataId and WCMChnlDoc.docKind =2 and WCMChnlDoc.DOCSTATUS in(10) and WCMChnlDoc.ChnlId=11224) order by issuetime desc;
  2. +----+--------------------+---------------------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+-------------------------------------------------+-------+----------+-----------------------------+
  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  4. +----+--------------------+---------------------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+-------------------------------------------------+-------+----------+-----------------------------+
  5. | 1 | PRIMARY | WCMMetaTableWebSite | NULL | ALL | NULL | NULL | NULL | NULL | 44976 | 100.00 | Using where; Using filesort |
  6. | 2 | DEPENDENT SUBQUERY | WCMChnlDoc | NULL | ref | IX_WCMCHNLDOC_CHNL,IX_WCMCHNLDOC_CHNL_DOC,IX_WCMCHNLDOC_CHNL_ORDER,IX_WCMCHNLDOC_DK_ST_CI,IX_WCMCHNLDOC_DOCKIND,IX_WCMCHNLDOC_DS_CHNL,IX_WCMCHNLDOC_DOCID_MODAL,IX_WCMChnlDoc_OperTimeChnlStatus,IX_WCMChnlDoc_CrUserStatus,IX_WCMChnlDoc_SiteIdStatus,IX_WCMChnlDoc_PubTimeChnlStatus,IX_WCMChnlDoc_CrUserSiteStatus,IX_WCMChnlDoc_FullSite | IX_WCMCHNLDOC_CHNL_DOC | 8 | const,trswcmtest.WCMMetaTableWebSite.MetaDataId | 1 | 5.00 | Using where |
  7. +----+--------------------+---------------------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+-------------------------------------------------+-------+----------+-----------------------------+
  8. 2 rows in set, 2 warnings (0.00 sec)
  9. root@dbmonitor-qb-01 10:58: [trswcmtest]> show warnings;
  10. +-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  11. | Level | Code | Message |
  12. +-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  13. | Note | 1276 | Field or reference 'trswcmtest.WCMMetaTableWebSite.MetaDataId' of SELECT #2 was resolved in SELECT #1 |
  14. | Note | 1003 | /* select#1 */ select `trswcmtest`.`wcmmetatablewebsite`.`MetaDataId` AS `MetaDataId` from `trswcmtest`.`wcmmetatablewebsite` where exists(/* select#2 */ select `trswcmtest`.`wcmchnldoc`.`DOCID` from `trswcmtest`.`wcmchnldoc` where ((`trswcmtest`.`wcmchnldoc`.`CHNLID` = 11224) and (`trswcmtest`.`wcmchnldoc`.`DOCSTATUS` = 10) and (`trswcmtest`.`wcmchnldoc`.`DOCKIND` = 2) and (`trswcmtest`.`wcmchnldoc`.`MODAL` >= 1) and (`trswcmtest`.`wcmchnldoc`.`DOCID` = `trswcmtest`.`wcmmetatablewebsite`.`MetaDataId`))) order by `trswcmtest`.`wcmmetatablewebsite`.`IssueTime` desc |
  15. +-------+------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  16. 2 rows in set (0.00 sec)

从执行计划看,驱动表选择的是wcmmetatablewebsite,该表数据量为63093,而被驱动表为wcmchnldoc,加上谓词条件的结果集要远小于结果集wcmmetatablewebsite,并且可以看到表wcmmetatablewebsite为全表扫描,效率非常低,这一点其实从后面的profile的结果也可以看出来

三、改写后SQL

  1. root@dbmonitor-qb-01 10:58: [trswcmtest]> desc SELECT w.MetaDataId FROM WCMMetaTableWebSite w INNER JOIN WCMChnlDoc d ON w.MetaDataId=DocId WHERE d.Modal>=1 AND d.docKind =2 AND d.DOCSTATUS IN(10) AND d.ChnlId=11224 ORDER BY issuetime DESC;
  2. +----+-------------+-------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+--------------------+------+----------+----------------------------------------------+
  3. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
  4. +----+-------------+-------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+--------------------+------+----------+----------------------------------------------+
  5. | 1 | SIMPLE | d | NULL | ref | IX_WCMCHNLDOC_CHNL,IX_WCMCHNLDOC_CHNL_DOC,IX_WCMCHNLDOC_CHNL_ORDER,IX_WCMCHNLDOC_DK_ST_CI,IX_WCMCHNLDOC_DOCKIND,IX_WCMCHNLDOC_DS_CHNL,IX_WCMCHNLDOC_DOCID_MODAL,IX_WCMChnlDoc_OperTimeChnlStatus,IX_WCMChnlDoc_CrUserStatus,IX_WCMChnlDoc_SiteIdStatus,IX_WCMChnlDoc_PubTimeChnlStatus,IX_WCMChnlDoc_CrUserSiteStatus,IX_WCMChnlDoc_FullSite | IX_WCMCHNLDOC_DK_ST_CI | 13 | const,const,const | 3 | 33.33 | Using where; Using temporary; Using filesort |
  6. | 1 | SIMPLE | w | NULL | ref | IX_WebSite_282457558 | IX_WebSite_282457558 | 5 | trswcmtest.d.DOCID | 1 | 100.00 | NULL |
  7. +----+-------------+-------+------------+------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+------------------------+---------+--------------------+------+----------+----------------------------------------------+
  8. 2 rows in set, 1 warning (0.00 sec)
  9. root@dbmonitor-qb-01 10:58: [trswcmtest]> show warnings;
  10. +-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  11. | Level | Code | Message |
  12. +-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  13. | Note | 1003 | /* select#1 */ select `trswcmtest`.`w`.`MetaDataId` AS `MetaDataId` from `trswcmtest`.`wcmmetatablewebsite` `w` join `trswcmtest`.`wcmchnldoc` `d` where ((`trswcmtest`.`w`.`MetaDataId` = `trswcmtest`.`d`.`DOCID`) and (`trswcmtest`.`d`.`CHNLID` = 11224) and (`trswcmtest`.`d`.`DOCSTATUS` = 10) and (`trswcmtest`.`d`.`DOCKIND` = 2) and (`trswcmtest`.`d`.`MODAL` >= 1)) order by `trswcmtest`.`w`.`IssueTime` desc |
  14. +-------+------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  15. 1 row in set (0.00 sec)

改写后的执行计划看到驱动表由wcmmetatablewebsite变为了wcmchnldoc,并且都用到了索引,小结果集驱动大结果集

四、profile结果

  1. root@dbmonitor-qb-01 11:00: [trswcmtest]> show profiles;
  2. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  3. | Query_ID | Duration | Query |
  4. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  5. | 1 | 1.29911175 | select WCMMetaTableWebSite.MetaDataId from WCMMetaTableWebSite where exists(select WCMChnlDoc.DocId from WCMChnlDoc where WCMChnlDoc.ChnlId>=1 and WCMChnlDoc.Modal>=1 and WCMChnlDoc.DocId=WCMMetaTableWebSite.MetaDataId and WCMChnlDoc.docKind =2 and WCMChnlDoc.DOCSTATUS in(10) and WCMChnlDoc.ChnlId= |
  6. | 2 | 0.00011800 | show profilings |
  7. | 3 | 0.00022575 | set profiling=0l |
  8. | 4 | 1.34196650 | select WCMMetaTableWebSite.MetaDataId from WCMMetaTableWebSite where exists(select WCMChnlDoc.DocId from WCMChnlDoc where WCMChnlDoc.ChnlId>=1 and WCMChnlDoc.Modal>=1 and WCMChnlDoc.DocId=WCMMetaTableWebSite.MetaDataId and WCMChnlDoc.docKind =2 and WCMChnlDoc.DOCSTATUS in(10) and WCMChnlDoc.ChnlId= |
  9. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  10. 4 rows in set, 1 warning (0.00 sec)
  11. root@dbmonitor-qb-01 11:01: [trswcmtest]> show profile for query 4;
  12. +--------------------+----------+
  13. | Status | Duration |
  14. +--------------------+----------+
  15. | executing | 0.000007 |
  16. | Sending data | 0.000018 |
  17. | executing | 0.000007 |
  18. | Sending data | 0.000011 |
  19. | executing | 0.000007 |
  20. | Sending data | 0.000011 |
  21. | executing | 0.000007 |
  22. | Sending data | 0.000011 |
  23. | executing | 0.000007 |
  24. | Sending data | 0.000011 |
  25. | executing | 0.000007 |
  26. | Sending data | 0.000011 |
  27. | executing | 0.000008 |
  28. | Sending data | 0.000011 |
  29. | executing | 0.000007 |
  30. | Sending data | 0.000011 |
  31. | executing | 0.000007 |
  32. | Sending data | 0.000011 |
  33. | executing | 0.000021 |
  34. | Sending data | 0.000019 |
  35. | executing | 0.000007 |
  36. | Sending data | 0.000040 |
  37. | executing | 0.000007 |
  38. | Sending data | 0.000011 |
  39. | executing | 0.000007 |
  40. | Sending data | 0.000010 |
  41. | executing | 0.000007 |
  42. | Sending data | 0.000012 |
  43. | executing | 0.000007 |
  44. | Sending data | 0.000011 |
  45. | executing | 0.000007 |
  46. | Sending data | 0.000011 |
  47. | executing | 0.000007 |
  48. | Sending data | 0.000011 |
  49. | executing | 0.000007 |
  50. | Sending data | 0.000011 |
  51. | executing | 0.000007 |
  52. | Sending data | 0.000016 |
  53. | executing | 0.000007 |
  54. | Sending data | 0.000011 |
  55. | executing | 0.000007 |
  56. | Sending data | 0.000011 |
  57. | executing | 0.000007 |
  58. | Sending data | 0.000011 |
  59. | executing | 0.000007 |
  60. | Sending data | 0.000011 |
  61. | executing | 0.000007 |
  62. | Sending data | 0.000011 |
  63. | executing | 0.000007 |
  64. | Sending data | 0.000011 |
  65. | executing | 0.000007 |
  66. | Sending data | 0.000011 |
  67. | executing | 0.000007 |
  68. | Sending data | 0.000011 |
  69. | executing | 0.000007 |
  70. | Sending data | 0.000017 |
  71. | executing | 0.000007 |
  72. | Sending data | 0.000011 |
  73. | executing | 0.000007 |
  74. | Sending data | 0.000011 |
  75. | executing | 0.000007 |
  76. | Sending data | 0.000011 |
  77. | executing | 0.000007 |
  78. | Sending data | 0.000011 |
  79. | executing | 0.000007 |
  80. | Sending data | 0.000011 |
  81. | executing | 0.000007 |
  82. | Sending data | 0.000011 |
  83. | executing | 0.000007 |
  84. | Sending data | 0.000011 |
  85. | executing | 0.000007 |
  86. | Sending data | 0.000011 |
  87. | executing | 0.000007 |
  88. | Sending data | 0.000019 |
  89. | executing | 0.000007 |
  90. | Sending data | 0.000011 |
  91. | executing | 0.000007 |
  92. | Sending data | 0.000011 |
  93. | executing | 0.000007 |
  94. | Sending data | 0.000011 |
  95. | executing | 0.000007 |
  96. | Sending data | 0.000011 |
  97. | executing | 0.000007 |
  98. | Sending data | 0.000011 |
  99. | executing | 0.000007 |
  100. | Sending data | 0.000011 |
  101. | executing | 0.000007 |
  102. | Sending data | 0.000029 |
  103. | executing | 0.000007 |
  104. | Sending data | 0.000012 |
  105. | executing | 0.000007 |
  106. | Sending data | 0.000013 |
  107. | executing | 0.000007 |
  108. | Sending data | 0.000035 |
  109. | end | 0.000009 |
  110. | query end | 0.000014 |
  111. | closing tables | 0.000015 |
  112. | freeing items | 0.000027 |
  113. | logging slow query | 0.000060 |
  114. | cleaning up | 0.000020 |
  115. +--------------------+----------+
  116. 100 rows in set, 1 warning (0.00 sec)
  117. root@dbmonitor-qb-01 11:01: [trswcmtest]> SELECT w.MetaDataId FROM WCMMetaTableWebSite w LEFT JOIN WCMChnlDoc d ON w.MetaDataId=DocId WHERE d.Modal>=1 AND d.docKind =2 AND d.DOCSTATUS IN(10) AND d.ChnlId=11224 ORDER BY issuetime DESC;
  118. +------------+
  119. | MetaDataId |
  120. +------------+
  121. | 736271 |
  122. | 736261 |
  123. | 735298 |
  124. +------------+
  125. 3 rows in set (0.00 sec)
  126. root@dbmonitor-qb-01 11:01: [trswcmtest]> show profiles;
  127. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  128. | Query_ID | Duration | Query |
  129. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  130. | 1 | 1.29911175 | select WCMMetaTableWebSite.MetaDataId from WCMMetaTableWebSite where exists(select WCMChnlDoc.DocId from WCMChnlDoc where WCMChnlDoc.ChnlId>=1 and WCMChnlDoc.Modal>=1 and WCMChnlDoc.DocId=WCMMetaTableWebSite.MetaDataId and WCMChnlDoc.docKind =2 and WCMChnlDoc.DOCSTATUS in(10) and WCMChnlDoc.ChnlId= |
  131. | 2 | 0.00011800 | show profilings |
  132. | 3 | 0.00022575 | set profiling=0l |
  133. | 4 | 1.34196650 | select WCMMetaTableWebSite.MetaDataId from WCMMetaTableWebSite where exists(select WCMChnlDoc.DocId from WCMChnlDoc where WCMChnlDoc.ChnlId>=1 and WCMChnlDoc.Modal>=1 and WCMChnlDoc.DocId=WCMMetaTableWebSite.MetaDataId and WCMChnlDoc.docKind =2 and WCMChnlDoc.DOCSTATUS in(10) and WCMChnlDoc.ChnlId= |
  134. | 5 | 0.00132675 | SELECT w.MetaDataId FROM WCMMetaTableWebSite w LEFT JOIN WCMChnlDoc d ON w.MetaDataId=DocId WHERE d.Modal>=1 AND d.docKind =2 AND d.DOCSTATUS IN(10) AND d.ChnlId=11224 ORDER BY issuetime DESC |
  135. +----------+------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  136. 5 rows in set, 1 warning (0.00 sec)
  137. root@dbmonitor-qb-01 11:01: [trswcmtest]> show profile for query 5;
  138. +----------------------+----------+
  139. | Status | Duration |
  140. +----------------------+----------+
  141. | starting | 0.000119 |
  142. | checking permissions | 0.000020 |
  143. | checking permissions | 0.000020 |
  144. | Opening tables | 0.000036 |
  145. | init | 0.000055 |
  146. | System lock | 0.000027 |
  147. | optimizing | 0.000049 |
  148. | statistics | 0.000518 |
  149. | preparing | 0.000039 |
  150. | Creating tmp table | 0.000042 |
  151. | Sorting result | 0.000021 |
  152. | executing | 0.000018 |
  153. | Sending data | 0.000131 |
  154. | Creating sort index | 0.000045 |
  155. | end | 0.000019 |
  156. | query end | 0.000025 |
  157. | removing tmp table | 0.000023 |
  158. | query end | 0.000018 |
  159. | closing tables | 0.000024 |
  160. | freeing items | 0.000036 |
  161. | cleaning up | 0.000044 |
  162. +----------------------+----------+
  163. 21 rows in set, 1 warning (0.00 sec)

小小总结一下:
使用MySQL的过程中,尽量用关联代替子查询提高效率,尤其是更新或者删除数据时少用子查询包括exsits,一定要用关联来代替