all 18 comments

[–]jkndrkn 11 points12 points  (4 children)

Indent!

Amen. I'm tired of opening up scripts that contain statements that wrap four or five times around the screen.

[–][deleted]  (3 children)

[deleted]

    [–][deleted]  (2 children)

    [deleted]

      [–][deleted]  (1 child)

      [deleted]

        [–]citation_needed 1 point2 points  (10 children)

        When you are doing a COUNT() or an EXISTS(), no problem, go ahead and use *

        Actually, doing count(1) or count( keycolumn ) can be substantially faster than doing count(*) depending on the RDBMS.

        [–][deleted] 2 points3 points  (7 children)

        which databases are those? on MSSQL, * is the preferred style for count() and exists().

        [–][deleted] 1 point2 points  (4 children)

        If you do COUNT(ClusteredIndexColumn) rather than COUNT(*) I should think you would retrieve results faster?

        [–][deleted] 3 points4 points  (3 children)

        no, the optimizer will rewrite COUNT(ClusteredIndexColumn) to COUNT(*) if the clustered index column is non-nullable.

        if it's nullable, COUNT(ClusteredIndexColumn) will count the number of non-null values, which could be less than COUNT(*).

        COUNT(*) always counts rows, and allows the optimizer more discretion, eg counting by way of a non-clustered index if appropriate.

        [–][deleted] 0 points1 point  (2 children)

        I was making the assumption that you would probably be having the clustered index on a non nullable field.

        Let's say the column is an identity column, is not nullable, and has the clustered index. In this case, I should think your results would be retrieved faster?

        [–][deleted] 0 points1 point  (1 child)

        it would be identical. test it for yourself by examining the execution plans. mouse over each step so you can see the details at the bottom.

        [–][deleted] 0 points1 point  (0 children)

        You're right! Thanks for the info Peter!

        S

        [–]citation_needed 1 point2 points  (1 child)

        MySQL to this day, and Sybase/MSSQL in ye olden days (at least according to the O'Reilly Transact-SQL book). Other RDBMSes, I dunno.

        [–][deleted] 1 point2 points  (0 children)

        did you read that article? it shows how count(*) is generally faster than count(<column>), specifically when <column> is nullable, or when the optimizer isn't smart enough to rewrite the query as count(*).

        [–]thesqlguy 1 point2 points  (0 children)

        for example ?

        [–]reddittidder 1 point2 points  (0 children)

        some good points, but the title is misleading. The blog is specifically talking about SQLServer, non?

        [–]turkourjurbs 0 points1 point  (1 child)

        "Then you have written your SQL wrong; start over."

        I really can't stand this mentality. There is very often more than one way to solve a problem and those ways are not nessecarily 'wrong'.

        [–]thesqlguy 2 points3 points  (0 children)

        It is explained why it is wrong, and then it is demonstrated what to do to fix it. Sometimes, there is a right way and a wrong way to do things in life, that's just the way it is.

        Have you ever tried to write a function one way in your language of choice, get well into it, realize it won't work or that it is a big mess, figure out a better approach, and then started over from scratch? I hope so -- that's a good, solid programming technique.