sql查詢慢最佳化

來源:互聯網
上載者:User

標籤:需要   設定   class   分享   company   where   運行時   alt   為什麼   

SELECTg.goods_id,g.type_id,g.user_id,g.productname,g.img,g.intro,g.attr,u.companyname,u.enloginname,u.useridFROMsite_goods g FORCE,ucenter_member uWHERE( `type_id` IN ( ‘13‘, ‘5634‘, ‘5674‘, ‘5675‘, ‘5676‘, ‘5677‘, ‘5678‘, ‘5679‘, ‘5680‘, ‘5681‘, ‘5635‘, ‘5639‘, ‘5795‘,
‘5796‘, ‘5797‘, ‘5798‘, ‘5799‘, ‘5800‘, ‘5645‘, ‘5992‘, ‘5993‘, ‘5994‘, ‘5995‘, ‘5996‘, ‘6035‘, ‘6036‘, ‘6037‘, ‘5646‘, ‘5726‘,
‘5727‘, ‘5899‘, ‘5900‘, ‘5964‘, ‘5648‘, ‘5652‘, ‘5653‘, ‘5724‘, ‘5801‘, ‘5803‘, ‘5804‘, ‘5654‘, ‘5728‘, ‘5988‘, ‘5990‘, ‘5991‘,
‘6012‘, ‘6013‘, ‘6014‘, ‘5655‘, ‘5659‘, ‘5940‘, ‘6074‘, ‘6075‘, ‘6076‘, ‘6077‘, ‘6078‘, ‘6080‘, ‘5660‘, ‘5774‘, ‘5775‘, ‘5776‘,
‘5777‘, ‘5778‘, ‘5779‘, ‘5780‘, ‘5781‘, ‘5963‘, ‘5661‘, ‘5782‘, ‘5783‘, ‘5784‘, ‘5785‘, ‘5786‘, ‘5662‘, ‘5787‘, ‘5788‘, ‘5789‘
, ‘5790‘, ‘5791‘, ‘5792‘, ‘5941‘, ‘5663‘, ‘5638‘, ‘5673‘, ‘5793‘, ‘5794‘, ‘5989‘, ‘5664‘, ‘5665‘, ‘5667‘, ‘5668‘, ‘5671‘, ‘5683‘,
‘5688‘, ‘5691‘, ‘5692‘, ‘5696‘, ‘5697‘, ‘5698‘, ‘5760‘, ‘5761‘, ‘5762‘, ‘5693‘, ‘5694‘, ‘5763‘, ‘6003‘, ‘6050‘, ‘5695‘, ‘5699‘, ‘5700‘, ‘5701‘, ‘5702‘, ‘5703‘, ‘5758‘, ‘5759‘, ‘6008‘, ‘5705‘, ‘5706‘, ‘5707‘, ‘5708‘, ‘5709‘, ‘5764‘, ‘5895‘, ‘6004‘, ‘6038‘, ‘5710‘, ‘5711‘, ‘5713‘, ‘5765‘, ‘5766‘, ‘5834‘, ‘5850‘, ‘5851‘, ‘5852‘, ‘5714‘, ‘6066‘, ‘6067‘, ‘6068‘, ‘6069‘, ‘6070‘, ‘6071‘, ‘6072‘, ‘6073‘, ‘5716‘, ‘11406‘, ‘11407‘, ‘11408‘, ‘11409‘, ‘11410‘, ‘11411‘, ‘11412‘, ‘11413‘, ‘11414‘, ‘11415‘, ‘11416‘, ‘11417‘, ‘11418‘, ‘11419‘, ‘11420‘, ‘11421‘, ‘5723‘, ‘5725‘, ‘5729‘, ‘5730‘, ‘5802‘, ‘5715‘, ‘5942‘, ‘5943‘, ‘5944‘, ‘5946‘, ‘5947‘, ‘5913‘, ‘5914‘, ‘5915‘, ‘5916‘, ‘5917‘, ‘5918‘, ‘5919‘, ‘5920‘, ‘5930‘, ‘5997‘, ‘6000‘, ‘5945‘, ‘6001‘, ‘6002‘, ‘6005‘, ‘6006‘, ‘11278‘, ‘6007‘, ‘5965‘, ‘6079‘, ‘6009‘, ‘5650‘, ‘5656‘, ‘6011‘, ‘6010‘, ‘5636‘, ‘5642‘, ‘5643‘, ‘5644‘, ‘5658‘, ‘5704‘, ‘6015‘, ‘6016‘, ‘6017‘, ‘6018‘, ‘6019‘, ‘6020‘, ‘6021‘, ‘6022‘, ‘6023‘, ‘6024‘, ‘6025‘, ‘6026‘, ‘6027‘, ‘6028‘, ‘6029‘, ‘6030‘, ‘6031‘, ‘6032‘, ‘6033‘, ‘6081‘, ‘6082‘, ‘6083‘, ‘6084‘, ‘6085‘, ‘6086‘, ‘5657‘, ‘5666‘, ‘5670‘, ‘6039‘, ‘6087‘, ‘6088‘, ‘6089‘, ‘11267‘, ‘11268‘, ‘11269‘, ‘11270‘, ‘11271‘, ‘11272‘, ‘11273‘, ‘11274‘, ‘11275‘, ‘11276‘, ‘11277‘, ‘11279‘, ‘11280‘, ‘11281‘, ‘11282‘, ‘11283‘, ‘11284‘, ‘11285‘, ‘11286‘, ‘11287‘, ‘11288‘, ‘11289‘, ‘11290‘, ‘11291‘, ‘11292‘, ‘11293‘, ‘11294‘, ‘11295‘, ‘11296‘, ‘11297‘, ‘11298‘, ‘11299‘, ‘11300‘, ‘11301‘, ‘11302‘, ‘11303‘, ‘11304‘, ‘11305‘, ‘11306‘, ‘11307‘, ‘11308‘, ‘11309‘, ‘11310‘, ‘11311‘, ‘11312‘, ‘11313‘, ‘11314‘, ‘11315‘, ‘11316‘, ‘11317‘, ‘11318‘, ‘11319‘, ‘11320‘, ‘11321‘, ‘11322‘, ‘11323‘, ‘11324‘, ‘11325‘, ‘11326‘, ‘11327‘, ‘11328‘, ‘11329‘, ‘11330‘, ‘11331‘, ‘11332‘, ‘11333‘, ‘11334‘, ‘11335‘, ‘11336‘, ‘11337‘, ‘11338‘, ‘11339‘, ‘11340‘, ‘11341‘, ‘11342‘, ‘11343‘, ‘11344‘, ‘11345‘, ‘11346‘, ‘11347‘, ‘11348‘, ‘11349‘, ‘11350‘, ‘11351‘, ‘11352‘, ‘11353‘, ‘11354‘, ‘11355‘, ‘11356‘, ‘11357‘, ‘11358‘, ‘11359‘, ‘11360‘, ‘11361‘, ‘11362‘, ‘11363‘, ‘11364‘, ‘11365‘, ‘11366‘, ‘11367‘, ‘11368‘, ‘11369‘, ‘11370‘, ‘11371‘, ‘11372‘, ‘11373‘, ‘11374‘, ‘11375‘, ‘11376‘, ‘11377‘, ‘11378‘, ‘11379‘, ‘11380‘, ‘11381‘, ‘11382‘, ‘11383‘, ‘11384‘, ‘11385‘, ‘11386‘, ‘11387‘, ‘11388‘, ‘11389‘, ‘11390‘, ‘11391‘, ‘11392‘, ‘11393‘, ‘11394‘, ‘11395‘, ‘11396‘, ‘11397‘, ‘11398‘, ‘11399‘, ‘11400‘, ‘11401‘, ‘11402‘, ‘11403‘, ‘11404‘, ‘11405‘, ‘11423‘, ‘11424‘, ‘11425‘, ‘11426‘, ‘11427‘, ‘11428‘, ‘11429‘, ‘11430‘, ‘11431‘, ‘11432‘, ‘11433‘, ‘11434‘, ‘11435‘, ‘11436‘, ‘11437‘, ‘11438‘, ‘11439‘, ‘11440‘, ‘11441‘, ‘11442‘, ‘11443‘, ‘11444‘, ‘11445‘, ‘11446‘, ‘11447‘, ‘11448‘, ‘11449‘, ‘11450‘, ‘11451‘ )AND (g.user_id = u.useridAND g.is_up = ‘true‘AND g.checks = 1AND g.disabled = ‘false‘AND u.disabled = ‘false‘AND u.checkid = 2)LIMIT 50

  上面的sql運行幾十秒,因為資料有點多,然後我用explan。

走的索引是ind_is_up

然後查看了is_up的分組多少資料,其實全都是true,所以這裡的 ind_is_up其實沒什麼意思。

所以我們這裡要重新去讓sql走索引。FORCE INDEX(uni_type_id), (強制索引)

修改sql中的from塊的語句

FROM
site_goods g FORCE INDEX(uni_type_id),
ucenter_member u

為什麼選uni_type_id這個是根據實際情況來的,因為上面的sql中where中索引都不是很好,就type_id 分組的比較多。

這樣語句就快很了。來看看我們現在的explan

這裡走的索引就是uni_type了。

總結: mysql運行時,只會使用一個索引,但是mysql選擇的索引也會不是我們理想的索引,這時候就需要我們來強制設定索引。

 

sql查詢慢最佳化

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.