简体   繁体   中英

Mysql query working very slow How can optimize query?

Query:

SELECT
  `item`.*,
  GROUP_CONCAT(DISTINCT category.English_name) AS category,
  GROUP_CONCAT(DISTINCT item_color.Color) AS Color,
  GROUP_CONCAT(
    DISTINCT sub_category.English_name
  ) AS Sub_category,
  GROUP_CONCAT(DISTINCT unit.Name) AS Unit,
  GROUP_CONCAT(DISTINCT item_unit.Value) AS Unit_value,
  `gst`.`HSN` AS `GST_hsn`,
  `base_unit`.`Name` AS `Base_unit`
FROM
  `item`
LEFT JOIN
  `gst` ON `gst`.`ID` = `item`.`GST_hsn`
LEFT JOIN
  `item_category` ON `item_category`.`Item` = `item`.`ID`
LEFT JOIN
  `category` ON `category`.`ID` = `item_category`.`Category`
LEFT JOIN
  `item_color` ON `item_color`.`Item` = `item`.`ID`
LEFT JOIN
  `item_sub_category` ON `item_sub_category`.`Item` = `item`.`ID`
LEFT JOIN
  `sub_category` ON `sub_category`.`ID` = `item_sub_category`.`Sub_category`
LEFT JOIN
  `item_unit` ON `item_unit`.`Item` = `item`.`ID`
LEFT JOIN
  `unit` ON `unit`.`ID` = `item_unit`.`Unit`
LEFT JOIN
  `unit` AS `base_unit` ON `base_unit`.`ID` = `item`.`Base_unit`
WHERE
  `item`.`Status` = 'Open'
GROUP BY
  `item`.`ID`

If you want to visit schema. Please visit DB schema on sqlfiddle.

http://sqlfiddle.com/#!9/c1b1e0/1

I have more than 1500 rows it taking almost 5 sec for execution How can I optimize this query so this query takes less than 1 sec.

The lack of suitable indexes in the several many-to-many tables is a significant performance problem. Follow the advice in

http://mysql.rjweb.org/doc.php/index_cookbook_mysql#many_to_many_mapping_table

which says

Do it this way. (Of course, adapt it to your particular table and column names.)

CREATE TABLE XtoY (
    # No surrogate id for this table
    x_id MEDIUMINT UNSIGNED NOT NULL,   -- For JOINing to one table
    y_id MEDIUMINT UNSIGNED NOT NULL,   -- For JOINing to the other table
    # Include other fields specific to the 'relation'
    PRIMARY KEY(x_id, y_id),            -- When starting with X
    INDEX      (y_id, x_id)             -- When starting with Y
) ENGINE=InnoDB;

Notes:

⚈  Lack of an AUTO_INCREMENT id for this table -- The PK given is the 'natural' PK; there is no good reason for a surrogate.
⚈  "MEDIUMINT" -- This is a reminder that all INTs should be made as small as is safe (smaller ⇒ faster). Of course the declaration here must match the definition in the table being linked to.
⚈  "UNSIGNED" -- Nearly all INTs may as well be declared non-negative
⚈  "NOT NULL" -- Well, that's true, isn't it?
⚈  "InnoDB" -- More effecient than MyISAM because of the way the PRIMARY KEY is clustered with the data in InnoDB.
⚈  "INDEX(y_id, x_id)" -- The PRIMARY KEY makes it efficient to go one direction; this index makes the other direction efficient. No need to say UNIQUE; that would be extra effort on INSERTs.
⚈  In the secondary index, saying just INDEX(y_id) would work because it would implicit include x_id. But I would rather make it more obvious that I am hoping for a 'covering' index.

To conditionally INSERT new links, use IODKU

Note that if you had an AUTO_INCREMENT in this table, IODKU would "burn" ids quite rapidly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM