[Solved] faster update query in mysql

EverSQL Database Performance Knowledge Base

faster update query in mysql

Database type:

can any body suggest which one would be faster for execution(Consider handling large data more than 2 Millions rows)

UPDATE first_table ft, second_table st SET ft.Dataset=3
WHERE st.Dataset =0
AND (ft.Dataset =1 || ft.Dataset=2)
AND (ft.ID IS NULL )
AND st.First_Name = ft.First_Name
AND st.Last_Name = ft.Last_Name
AND st.Zip = ft.Zip

OR

putting following into two different update query

(ft.Dataset =1 || ft.Dataset=2)  

How to optimize this SQL query?

The following recommendations will help you in your 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 `first_table` ADD INDEX `first_table_idx_datase_id_first_last_zip` (`Dataset`,`ID`,`First_Name`,`Last_Name`,`Zip`);
ALTER TABLE `second_table` ADD INDEX `second_table_idx_dataset_first_last_na_zip` (`Dataset`,`First_Name`,`Last_Name`,`Zip`);
The optimized query:
SELECT
        ft.Dataset 
    FROM
        first_table ft,
        second_table st 
    WHERE
        st.Dataset = 0 
        AND (
            ft.Dataset = 1 
            OR ft.Dataset = 2
        ) 
        AND (
            ft.ID IS NULL
        ) 
        AND st.First_Name = ft.First_Name 
        AND st.Last_Name = ft.Last_Name 
        AND st.Zip = ft.Zip

Related Articles



* original question posted on StackOverflow here.