开发者

Optimize MySQL UPDATE Query

开发者 https://www.devze.com 2023-04-10 07:29 出处:网络
I\'m trying to run this query, and the table that\'s being updated has about 10,000 rows. The query takes so long to execute that I can\'t even be bothered to wait for the return.

I'm trying to run this query, and the table that's being updated has about 10,000 rows. The query takes so long to execute that I can't even be bothered to wait for the return.

In a couple hours this table will have 100,000 rows and so, it's going to take 10 times longer than it does already. Anyone have any ideas to optimize it?

UPDATE

`wpsapi4`.`product_details` AS `pd`,
`r2r`.`partmaster` AS `pm`,
`r2r`.`partpriceinv` AS `ppi`,
`r2r`.`manufacturer` AS `m`

SET

`pd`.`product_name`=`pm`.`ItemName`,
`pd`.`data_source`='R2R',
`pd`.`partmaster`=`pm`.`id`,
`pd`.`pu`=``.`ppi`.`DistributorPartNumberShort`,
`pd`.`de开发者_C百科scription_raw`=`pm`.`ItemDescription`,
`pd`.`dealer_price`=`ppi`.`MSRP`,
`pd`.`weight`=`pm`.`Weight`,
`pd`.`vendor_name`=`m`.`ManufacturerName`

WHERE

(
`pm`.`ManufacturerNumberShort`=`pd`.`vendor_number`
OR
`pm`.`ManufacturerNumberLong`=`pd`.`vendor_number`
)
AND
`pm`.`id`=`ppi`.`DistributorPartNumberShort`
AND
`ppi`.`DistributorID`=2
AND
`pm`.`ManufacturerID`=`m`.`id`

If you think it could be to do with the table structures then please say so, I can't really change the structure at this point but if you know where the indexes should be then that would be great. Indexes are already optimized on the r2r database.


The columns to index are the ones reference in your where clause.

Consider adding the following:

  1. An index on the pm.ManufacturerNumberShort column.
  2. An index on the pm.ManufacturerNumberLong column.
  3. An index on the pm.id column.
  4. An index on the pm.ManufacturerID column.
  5. An index on the ppi.DistributorPartNumberShort column.
  6. An index on the ppi.DistributorID column.

Based on input from Darhazer:

Consider adding one or more of the following:

  1. An index on the pm.ManufacturerNumberShort, pm.id, and pm.ManufacturerID columns.
  2. An index on the pm.ManufacturerNumberLong, pm.id, and pm.ManufacturerID columns.
  3. An index on the ppi.DistributorPartNumberShort and ppi.DistributorID columns.


You are doing an OR on Vendor Number, I would start by making sure that you have an index on vendor number on both tables.

Looks to me that the other columns already should have indexes.

0

精彩评论

暂无评论...
验证码 换一张
取 消