• Visitors can check out the Forum FAQ by clicking this link. You have to register before you can post: click the REGISTER link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. View our Forum Privacy Policy.
  • Want to receive the latest contracting news and advice straight to your inbox? Sign up to the ContractorUK newsletter here. Every sign up will also be entered into a draw to WIN £100 Amazon vouchers!

Efficient SQL to retrieve latest record

Collapse
X
  •  
  • Filter
  • Time
  • Show
Clear All
new posts

    Efficient SQL to retrieve latest record

    I need to retrieve the latest record in a table thus:

    Table: entries
    Columns:
    entry_date Date
    entry_number_for_date int --increments with each entry for a given entry date
    a_n_other_field char(10)

    there is an index defined as entry_date, entry_number_for_date

    Can anyone confirm whether this is the most efficient way of retrieve the latest record from this table please? Ta:


    set rowcount 1
    select * from entries
    order by entry_date desc, entry_number_for_date desc
    set rowcount 0
    "Experience hath shewn, that even under the best forms of government those entrusted with power have, in time, and by slow operations, perverted it into tyranny. "


    Thomas Jefferson

    #2
    I would suggest you use TOP 1 instead of setting rowcount (which is deprecated) and the can be optimised by the query optimiser where as rowcount cannot.

    When you create the index, make sure it is a descending index, so that the data in the index is already sorted in the same way as your order by clause.

    Do you do a lot of inserts? If so, having to maintain this index will slow down your inserts.

    HTH

    Comment


      #3
      Thanks DP - but I don't think top 1 is available on Sybase? If you know different could you advise of the SQL?

      Thanks,

      Rup.
      "Experience hath shewn, that even under the best forms of government those entrusted with power have, in time, and by slow operations, perverted it into tyranny. "


      Thomas Jefferson

      Comment


        #4
        Would it not be better to have a subquery bring the max (ie most recent) entry_date value in, and thus select the record with the same entry_date value

        select a.* from entries a
        where a.entry_date = (select max(b.entry_date) from entries b)
        ;

        If there is an index on entry_date this should be efficient.
        Speaking gibberish on internet talkboards since last Michaelmas. Plus here on Twitter

        Comment


          #5
          Originally posted by MrMark View Post
          Would it not be better to have a subquery bring the max (ie most recent) entry_date value in, and thus select the record with the same entry_date value

          select a.* from entries a
          where a.entry_date = (select max(b.entry_date) from entries b)
          ;

          If there is an index on entry_date this should be efficient.
          Possibly. What I tend to do when faced with two possible (reasonable) solutions is to implement both and then use the query plan feature of query analyser to compare the relative cost of both against real world data.

          Comment


            #6
            Originally posted by Ruprect View Post
            I need to retrieve the latest record in a table thus:

            Table: entries
            Columns:
            entry_date Date
            entry_number_for_date int --increments with each entry for a given entry date
            a_n_other_field char(10)

            there is an index defined as entry_date, entry_number_for_date

            Can anyone confirm whether this is the most efficient way of retrieve the latest record from this table please? Ta:


            set rowcount 1
            select * from entries
            order by entry_date desc, entry_number_for_date desc
            set rowcount 0
            Is the index clustered? Is there only 1 index?

            Comment


              #7
              Single index, clustered.

              Fair point about the query plans, was just wondering if there was a renowned best practice method.
              "Experience hath shewn, that even under the best forms of government those entrusted with power have, in time, and by slow operations, perverted it into tyranny. "


              Thomas Jefferson

              Comment


                #8
                I seem to remember that sybase, with a clustered index, searches all entries where you specify equals. You could try "> yesterday" as that is more efficient.

                I dont think there is best practice with sybase - I recently had a query that just would not use an index even when forced. There was a column logicallyDeleted which was usually "0" but was sometimes "1". It was not in the statistics. Specifying ="0" gives a 10% match - specifying !="1" gives 90%.

                It really is just trial and error.

                Somewhere I have a ppt "the day in the life of a query" - 170 pages long!!

                Comment


                  #9
                  Originally posted by MrMark View Post
                  Would it not be better to have a subquery bring the max (ie most recent) entry_date value in, and thus select the record with the same entry_date value

                  select a.* from entries a
                  where a.entry_date = (select max(b.entry_date) from entries b)
                  ;

                  If there is an index on entry_date this should be efficient.
                  This is what I would do, except that you also need another where clause to filter for the highest entry number for that date, else it would bring back all the entried for the max date.

                  Code:
                  select *
                  from entries
                  where entry_date = (select max(entry_date) from entries)
                  and entry_number_for_date = (select max(entry_number_for_date) from
                    entries where entry_date = (select max(entry_date) from entries))
                  I doubt this is the most efficient way though as you now have 2 sub queries and 1 subsub query. Could make it better by setting some variables to begin with?
                  It's about time I changed this sig...

                  Comment


                    #10
                    hmmm... what about using a group by and having?

                    eg: select top 1 a_n_other_field from entries
                    group by entry_date, entry_number_for_date
                    having entry_date=MAX(entry_date) and entry_number_for_date=
                    MAX(entry_number_for_date)

                    ?? bizarre I admit. But it could beat subqueries. (if it works)
                    Last edited by lilelvis2000; 13 May 2008, 15:30.
                    McCoy: "Medical men are trained in logic."
                    Spock: "Trained? Judging from you, I would have guessed it was trial and error."

                    Comment

                    Working...
                    X