i want to sort my Posts by dtModified (datetime modified) if its not null (post modified b4), else sort by dtPosted (datetime posted)
i want to sort my Posts by dtModified (datetime modified) if its not null
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It seems like I’m giving this advice three times a week here on SO, maybe I should just give up and let it go 🙂 Nah, I don’t think so:
Don’t use per-row functions in your column calculations (or
order byclauses) if you want your database to scale well. You should check performance for your particular case (measure, don’t guess) but doing calculations when reading the database will generally affect your ability to scale (this won’t matter for your address book database but the shops I work in have huge amounts of data).The number of “how do I get my DB to go faster?” questions far outweighs the number of “how do I use less space?” ones. It’s a well-trodden path to sacrifice disk space for performance.
The right time to do calculations is when the data changes. That way the cost of the changes is amortised across all reads.
My advice is to create another column such as
dtLastActionto contain the ordering value then use an insert/update trigger to set it to the same asdtModifiedif not null, ordtPostedifdtModifiedis null. Technically, this violates 3NF but that’s okay if you know what you’re doing, and the triggers guarantee data consistency in that case.Then index on the
dtLastActioncolumn and see the speed of your queries improve at the (lesser) cost of some extra work during inserts and updates. I say lesser because the vast majority of database tables are read more often than written (obviously this method is useless if your particular situation is one of the very rare exceptions).Alternatively, for this particular case, you could set
dtModifiedanddtPostedto the same value when creating the entry, meaning thatdtModifiedwill never be null. You can still detect posts that have never been modified by the fact that these two datetime values are identical.