Which DB design is faster: a unique index and INSERT IGNORE, or using SELECT to find existing records?

后端 未结 6 1968
长情又很酷
长情又很酷 2020-12-21 12:03

I have a table with just one column: userid.

When a user accesses a certain page, his userid is being inserted to the table. Userids are unique, so there shouldn\'t

6条回答
  •  失恋的感觉
    2020-12-21 12:26

    You should make it unique in any cases.

    Wether to check first using SELECT, depends on what scenario is most common. If you have new users all the time, and only occationally existing users, it might be overall faster for the system to just insert and catch the exception in the rare occations this happens, but exception is slower than check first and then insert, so if it is a common scenario that it is an existing user, you should allways check first with select.

提交回复
热议问题