[Solved] sql query to fetch data even if conditon not satisfied
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

sql query to fetch data even if conditon not satisfied

I am not good in sql so please don't scold me for such basic question. Currently I am doing a lot of homework on other technologies so I will improve sql as and when I get time to study it. Anyway let come to the point.

My question is for below query. I am trying to fetch data from three tables and it works fine as long as all conditions are satisfied but the problem is that for a posting it may happen that images are not present i.e not record for a posting in image table then post.post_id = img.post_id and img.sequence='1' condition fails and no row is returned but I want row to be returned even if no record present in images for the posting. In such case return null for image path column.

SELECT 
    img.path as path, pav.value_text as beds, post.post_id as postID 
FROM 
    postings post, post_attributes_values pav, images img 
where 
    post.post_id = pav.post_id and post.status !='expired' and pav.attr_id='33' and post.post_id = img.post_id and img.sequence='1' and post.post_id=49

If all condition satisfies then O/P comes like

    path                beds          postId
-----------------------------------------------
    saome path value     2             49

but if post.post_id = img.post_id and img.sequence='1' condition not satisfied then O/P should be like:

    path        beds          postId
----------------------------------------
    null         2              49

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.
  2. Use Numeric Column Types For Numeric Values (query line: 12): Referencing a numeric value (e.g. 33) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  3. Use Numeric Column Types For Numeric Values (query line: 14): Referencing a numeric value (e.g. 1) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `images` ADD INDEX `images_idx_sequence_post_id` (`sequence`,`post_id`);
ALTER TABLE `post_attributes_values` ADD INDEX `post_values_idx_attr_id_post_id` (`attr_id`,`post_id`);
ALTER TABLE `postings` ADD INDEX `postings_idx_post_id_status` (`post_id`,`status`);
The optimized query:
SELECT
        img.path AS path,
        pav.value_text AS beds,
        post.post_id AS postID 
    FROM
        postings post,
        post_attributes_values pav,
        images img 
    WHERE
        post.post_id = pav.post_id 
        AND post.status != 'expired' 
        AND pav.attr_id = '33' 
        AND post.post_id = img.post_id 
        AND img.sequence = '1' 
        AND post.post_id = 49

Related Articles



* original question posted on StackOverflow here.