[Solved] Performance : Between in FROM or in WHERE?

EverSQL Database Performance Knowledge Base

Performance : Between in FROM or in WHERE?

I ask myself what is the more performance method when you join table and use JOIN in clause FROM and when you need to use BETWEEN :

or

Exemple :

SELECT *
FROM Tab1 a inner join Tab2 b 
on (a.pk=b.fk_1 AND a.date between b.date1 and b.date2) 

Versus

SELECT *
FROM Tab1 a inner join Tab2 b 
on a.pk=b.fk_1  
WHERE a.date between b.date1 and b.date2

Which one is the best?

Thank

P.S : sorry for my bad english

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. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  2. 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 `Tab1` ADD INDEX `tab1_idx_date` (`date`);
ALTER TABLE `Tab2` ADD INDEX `tab2_idx_fk_1` (`fk_1`);
The optimized query:
SELECT
        * 
    FROM
        Tab1 a 
    INNER JOIN
        Tab2 b 
            ON (
                a.pk = b.fk_1 
                AND a.date BETWEEN b.date1 AND b.date2
            )

Related Articles



* original question posted on StackOverflow here.