I have a table with over 2.5 million records in production that keeps track of if a specific user has already viewed an job:
mysql> DESCRIBE job_views;
+--------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| job_id | int(11) | YES | | NULL | |
| session_id | varchar(255) | YES | MUL | NULL | |
| created_at | datetime | YES | | NULL | |
| updated_at | datetime | YES | | NULL | |
+--------------------+--------------+------+-----+---------+----------------+
Before adding a row to the table, I want to ensure that there isn't already a row with the same job_id
and session_id
. Currently the application queries the table for a row that matches (presently without an index, although an index on the two columns has been added), but I believe a unique constraint makes more sense.
Is there a difference between a SELECT
using the index and then an INSERT
if a row isn't found versus an INSERT
that fails because of a unique constraint? If so, which is preferable (or how can I make that determination)?