I would like to ask mysql added group by after other indexes invalidated, how to solve?

EXPLAIN SELECT
  s.id,
  s.name,
  s.price,
  s.unit,
  s.qty,
  s.remark,
  s.image,
  s.cuft,
  s.volume,
  s.status,
  s.vender_id AS venderId,
  s.booth_id AS boothId,
  s.booth_no AS boothNo,
  s.show_booth_no AS showBoothNo,
  s.name_en AS nameEn,
  s.bar_code AS barCode,
  s.product_code AS productCode,
  s.type_one_id AS typeOneId,
  s.type_one_name AS typeOneName,
  s.type_two_id AS typeTwoId,
  s.type_two_name AS typeTwoName,
  s.ep_price AS epPrice,
  s.pack_cn AS packCn,
  s.pack_en AS packEn,
  s.inner_box AS innerBox,
  s.out_chest_long AS outChestLong,
  s.out_chest_width AS outChestWidth,
  s.out_chest_height AS outChestHeight,
  s.pack_long AS packLong,
  s.pack_width AS packWidth,
  s.pack_height AS packHeight,
  s.sample_long AS sampleLong,
  s.sample_width AS sampleWidth,
  s.sample_height AS sampleHeight,
  s.can_supply AS canSupply,
  s.rough_weight AS roughWeight,
  s.net_weight AS netWeight,
  s.is_delete AS isDelete,
  s.create_by AS createBy,
  s.create_date AS createDate,
  s.update_by AS updateBy,
  s.update_date AS updateDate,
  s.credentials AS credentials,
  s.credentials_id AS credentialsId,
  s.vender_name AS venderName,
  s.vender_code AS venderCode,
  s.booth_no AS boothNo,
  (SELECT
    sl.name
  FROM
    sys_login sl
  WHERE id = s.update_by) AS updateName,
  (SELECT
    sl.name
  FROM
    sys_login sl
  WHERE id = s.create_by) AS createName,
  v.contact1 AS contact1,
  v.tel1 AS tel1,
  v.phone1 AS phone1,
  v.meet_phone AS meetPhone,
  v.fax AS fax,
  v.qq AS qq,
  v.sms_code AS smsCode,
  (SELECT
    bh.end_date
  FROM
    booth_hire bh
  WHERE bh.booth_no = s.booth_no
    AND bh.vender_id = s.vender_id
    AND bh.status = 0
  LIMIT 1) AS boothEndDate,
  (SELECT
    sl.name
  FROM
    sys_login sl
  WHERE sl.id = s.create_by) AS createName,
  (SELECT
    sl.name
  FROM
    sys_login sl
  WHERE sl.id = s.update_by) AS updateName,
  s.sold_out_by AS soldOutBy,
  s.sold_out_date AS soldOutDate,
  s.sold_out_type AS soldOutType
FROM
  sample s
  LEFT JOIN vender v
    ON s.vender_id = v.id
WHERE 1 = 1
  AND s.status != 3
  AND s.status != - 1
  AND s.is_delete = 0
group by s.vender_id
LIMIT 0,100

clipboard.png

clipboard.png

Mar.05,2021

first of all, writing join in select is super annoying. Why can't you put join in from? why is it not pleasing to the eye? there are createName and updateName to write twice?
then,! = will not take the index, there are gender fields like is_delete, if the Filter result set is basically very small, you can use the index, otherwise adding the index will not make much sense. At this time, if there is a group by field with a better choice, of course you will use the group index, because you have to go back to the table to query anyway.
So. Index invalidation is normal. If you must use it, add force index


Test table qxd_user number is an index column, reg_dev is a normal column without indexing

1. group by full table scan when no condition is added

clipboard.png

2.where,wheregroup by

clipboard.png

3.group by

clipboard.png

clipboard.png


you should have a http://sqlfiddle.com/-sharp!2/70129/2

Menu