When to use VARCHAR and DATE/DATETIME

前端 未结 5 1604
野趣味
野趣味 2020-11-29 00:40

We had this programming discussion on Freenode and this question came up when I was trying to use a VARCHAR(255) to store a Date Variable in this format: D/MM/YYYY. So the q

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-29 01:03

    When you'll have database with more than 2-3 million rows you'll know why it's better to use DATETIME than VARCHAR :)

    Simple answer is that with databases - processing power isn't a problem anymore. Just the database size is because of HDD's seek time.

    Basically with modern harddisks you can read about 100 records / second if they're read in random order (usually the case) so you must do everything you can to minimize DB size, because:

    • The HDD's heads won't have to "travel" this much
    • You'll fit more data in RAM

    In the end it's always HDD's seek times that will kill you. Eg. some simple GROUP BY query with many rows could take a couple of hours when done on disk compared to couple of seconds when done in RAM => because of seek times.

    For VARCHAR's you can't do any searches. If you hate the way how SQL deals with dates so much, just use unix timestamp in 32 bit integer field. You'll have (basically) all advantages of using SQL DATE field, you'll just have to manipulate and format dates using your choosen programming language, not SQL functions.

提交回复
热议问题