Storing flags in a DB

前端 未结 3 1534
無奈伤痛
無奈伤痛 2021-01-06 11:12

In my application, I\'d like the user to select his working days. then store them in the database. Of course my application will process the users\' data like: Is today a wo

3条回答
  •  太阳男子
    2021-01-06 11:30

    I have made the mistake of going with option 1, and given the opportunity to go back in time I would absolutely do it the other way.

    Your database almost certainly will not use an index to make bitwise queries on your bitmask. So if you want to find, say, everyone working Tuesdays, you're going to do an index scan every time. As your tables get large, this can destroy your performance. You can try to optimize around this by caching a SELECT DISTINCT(bitmaskfield) ahead of time, doing the bitmask logic in your own application, and turing it into an appropriate WHERE bitmaskfield IN (...) clause, but that quickly becomes unmaintainable as you then have to update your distinct-bitmask cache in every place that you change values in the database.

    The extra tables and joins may seem like a pain, but the bitmask will turn out worse. Trust me on this. Use your database as a database.

提交回复
热议问题