SELECT
g.goods_id,
g.type_id,
g.user_id,
g.productname,
g.img,
g.intro,
g.attr,
u.companyname,
u.enloginname,
u.userid
FROM
site_goods g FORCE,
ucenter_member u
WHERE
( `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.userid
AND g.is_up = 'true'
AND g.checks = 1
AND 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选择的索引也会不是我们理想的索引,这时候就需要我们来强制设置索引。