which one is best query?

  • first query

    select COUNT(id) from tbl_birthmaster1 where status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where isnull(pdffilepath,'')!='' and status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where cfcApprove=0 and status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where cfcApprove=1 and mcaApprove=0 and status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where FormType=1 and mcaApprove=1 and DRApprove=0 and status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where FormType=2 and mcaApprove=1 and SDOApprove=0 and status_info=1

    and zone_id=1

    select COUNT(id) from tbl_birthmaster1 where FormType=3 and mcaApprove=1 and CRApprove=0 and status_info=1

    and zone_id=1

    second one is--

    SELECT

    sum(CASE WHEN status_info = 1 and zone_id=1 then 1 else 0 end) AS count1,

    sum(CASE WHEN isnull(pdffilepath,'')!='' AND status_info=1 and zone_id=1 then 1 else 0 end) AS count2,

    sum(CASE WHEN cfcApprove=0 and status_info=1 and zone_id=1 then 1 else 0 end) AS count3,

    sum(CASE WHEN cfcApprove=1 and mcaApprove=0 and status_info=1 and zone_id=1 then 1 else 0 end) AS count4,

    sum(CASE WHEN FormType=1 and mcaApprove=1 and DRApprove=0 and status_info=1 and zone_id=1 then 1 else 0 end) AS count5,

    sum(CASE WHEN FormType=2 and mcaApprove=1 and SDOApprove=0 and status_info=1 and zone_id=1 then 1 else 0 end) AS count6,

    sum(CASE WHEN FormType=3 and mcaApprove=1 and CRApprove=0 and status_info=1 and zone_id=1 then 1 else 0 end) AS count7

    FROM tbl_birthmaster1

  • second one

  • I don't there is a choice of which is "best". The "first query" is not a single query, it is several of them. It depends on what you want as output. I would think the performance wise the second option would be faster.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • It depends also how many data do you have and what indexes are there.

  • Considering the low selectivity of the criteria, they're both going to making sucking sounds. If you index to increase their SELECT performance, they'll likely timeout on inserts because of that same low selectivity which will cause massive extent splits.

    Considering that, the second "query" probably stands a better chance because it'll only do 1 table scan. As has been pointed out, though, they first returns data in an unpivoted format and the second returns the data in a pivoted format.

    You might be able to get a bit more performance out of both is you change {where isnull(pdffilepath,'')!=''} to {where pdfilepath > ''} which will also knock out the nulls and removes the column from the function.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply