[Solved] MySQL Query to set unique numbers in column via data linked in 2nd table

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
Optimal indexes for this query:
ALTER TABLE `inventoryequipment` ADD INDEX `inventoryequipment_idx_upgra_level_str_dex_int_luk` (`upgradeslots`,`level`,`str`,`dex`,`int`,`luk`);
The optimized query:
SELECT
        inventoryitems.uniqueid 
    FROM
        inventoryitems 
    WHERE
        EXISTS (
            SELECT
                inventoryequipment.inventoryitemid 
            FROM
                inventoryequipment 
            WHERE
                inventoryequipment.upgradeslots = 0 && inventoryequipment.level = 0 && inventoryequipment.str = 0 && inventoryequipment.dex = 0 && inventoryequipment.int = 0 && inventoryequipment.luk = 0 && inventoryequipment.hp = 0 && inventoryequipment.mp = 0 && inventoryequipment.watk = 0 && inventoryequipment.wdef = 0 && inventoryequipment.mdef = 0 && inventoryequipment.acc = 0 && inventoryequipment.hands = 0 && inventoryequipment.speed = 0 && inventoryequipment.jump = 0 && inventoryequipment.ringid = -1 && inventoryequipment.locked = 0 && inventoryequipment.isRing = 0
        )

Related Articles



* original question posted on StackOverflow here.