SQL query Slow optimization

Source: Internet
Author: User

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 ', ' 5 ' 726 ',
' 5727 ', ' 5899 ', ' 5900 ', ' 5964 ', ' 5648 ', ' 5652 ', ' 5653 ', ' 5724 ', ' 5801 ', ' 5803 ', ' 5804 ', ' 5654 ', ' 5728 ', ' 5988 ', ' 5990 ', ' 5 ' 991 ',
' 6012 ', ' 6013 ', ' 6014 ', ' 5655 ', ' 5659 ', ' 5940 ', ' 6074 ', ' 6075 ', ' 6076 ', ' 6077 ', ' 6078 ', ' 6080 ', ' 5660 ', ' 5774 ', ' 5775 ', ' 5 ' 776 ',
' 5777 ', ' 5778 ', ' 5779 ', ' 5780 ', ' 5781 ', ' 5963 ', ' 5661 ', ' 5782 ', ' 5783 ', ' 5784 ', ' 5785 ', ' 5786 ', ' 5662 ', ' 5787 ', ' 5788 ', ' 5 ' 789 '
, ' 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 ', ' 5 ' 699 ', ' 5700 ', ' 5701 ', ' 5702 ', ' 5703 ', ' 5758 ', ' 5759 ', ' 6008 ', ' 5705 ', ' 5706 ', ' 5707 ', ' 5708 ', ' 5709 ', ' 5764 ', ' 5895 ', ' 600 ' 4 ', ' 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 ', ' 5 ' 943 ', ' 5944 ', ' 5946 ', ' 5947 ', ' 5913 ', ' 5914 ', ' 5915 ', ' 5916 ', ' 5917 ', ' 5918 ', ' 5919 ', ' 5920 ', ' 5930 ', ' 5997 ', ' 6000 ', ' 594 ' 5 ', ' 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 ', ' 1129 ' 2 ', ' 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 ', ' 1 ' 1333 ', ' 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 ', ' 11 ' 360 ', ' 11361 ', ' 11362 ',' 11363 ', ' 11364 ', ' 11365 ', ' 11366 ', ' 11367 ', ' 11368 ', ' 11369 ', ' 11370 ', ' 11371 ', ' 11372 ', ' 11373 ', ' 11374 ', ' 11375 ', ' 1137 ' 6 ', ' 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 ', ' 1 ' 1434 ', ' 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

The above SQL runs for dozens of seconds because the data is a bit more, and then I use Explan.

The index of walking is IND_IS_UP

And then look at the is_up of how much data, in fact, are all true, so the ind_is_up here is not really interesting.

So we're going to go back to the SQL index here. Force index (UNI_TYPE_ID), (forced index)

To modify the FROM block statement in SQL

From
Site_goods g Force INDEX (uni_type_id),
Ucenter_member u

Why choose uni_type_id This is based on the actual situation, because the above SQL where the index is not very good, the type_id grouping is more.

That's a pretty quick statement. Take a look at our current explan.

The index to walk here is uni_type.

Summary: When MySQL runs, only one index is used, but the index chosen by MySQL is not our ideal index, then we need to force the index to be set.

SQL query Slow optimization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.