Best practice question for MySQL: order by id or date?

前端 未结 3 960
北恋
北恋 2020-12-29 12:23

This is kind of a noobish question, but it\'s one that I\'ve never been given a straight answer on.

Suppose I have a DB table with the following fields and

相关标签:
3条回答
  • 2020-12-29 12:33

    Maybe is faster by id, but safer by datetime; use the latter if have performance issues add an index.

    0 讨论(0)
  • 2020-12-29 12:46

    If there is a chance you'll get two added with the same date, you'll probably need:

    SELECT balance FROM my_table ORDER BY date_added DESC,id DESC LIMIT 1;
    

    (note the 'descending' clause on both fields).

    However, you will need to take into account what you want to happen when someone adds an adjusting entry of the 2nd of February which is given the date 31st January to ensure the month of January is complete. It will have an ID greater than those made on the 1st of February.

    Generally, accounting systems just work on the date. Perhaps if you could tell us why the order is important, we could make other suggestions.


    In response to your comment:

    I would love to hear any other ideas or advice you might have, even if they're off-topic since I have zero knowledge of accounting-type database models.

    I would provide a few pieces of advice - this is all I could think of immediately, I usually spew forth much more "advice" with even less encouragement :-) The first two, more database-related than accounting-relared, are:

    First, do everything in third normal form and only revert if and when you have performance problems. This will save you a lot of angst with duplicate data which may get out of step. Even if you do revert, use triggers and other DBMS capabilities to ensure that data doesn't get out of step.

    An example, if you want to speed up your searches on a last_name column, you can create an upper_last_name column (indexed) then use that to locate records matching your already upper-cased search term. This will almost always be faster than the per-row function upper(last_name). You can use an insert/update trigger to ensure the upper_last_name is always set correctly and this incurs the cost only when the name changes, not every time you search.

    Secondly, don't duplicate data even across tables (like your current schema) unless you can use those same trigger-type tricks to guarantee the data won't get out of step. What will your customer do when you send them an invoice where the final balance doesn't match the starting balance plus purchases? That's not going to make your company look very professional :-)

    Thirdly (and this is more accounting-related), you generally don't need to worry about the number of transactions when calculating balances on the fly. That's because accounting systems usually have a roll-over function at year end which resets the opening balances.

    So you're usually never having to process more than a year's worth of data at once which, unless you're the US government or Microsoft, is not that onerous.

    0 讨论(0)
  • 2020-12-29 12:52

    Personally I'd never trust an autoincrement in that way. I'd sort by the date.

    I'm pretty sure that the ID is guaranteed to be unique, but not necessarily sequential and increasing.

    0 讨论(0)
提交回复
热议问题