True/False vs 0/1 in MySQL

后端 未结 4 1335
忘了有多久
忘了有多久 2020-12-02 19:59

Which is faster in a MySQL database? Booleans, or using zero and one to represent boolean values? My frontend just has a yes/no radio button.

4条回答
  •  长情又很酷
    2020-12-02 20:20

    If you are into performance, then it is worth using ENUM type. It will probably be faster on big tables, due to the better index performance.

    The way of using it (source: http://dev.mysql.com/doc/refman/5.5/en/enum.html):

    CREATE TABLE shirts (
        name VARCHAR(40),
        size ENUM('x-small', 'small', 'medium', 'large', 'x-large')
    );
    

    But, I always say that explaining the query like this:

    EXPLAIN SELECT * FROM shirts WHERE size='medium';
    

    will tell you lots of information about your query and help on building a better table structure. For this end, it is usefull to let phpmyadmin Propose a table table structure - but this is more a long time optimisation possibility, when the table is already filled with lots of data.

提交回复
热议问题