0

I have a table (id, location, timestamp, type, value)

location / timestamp / type can duplicate,
but location + timestamp + type is unique


1, "Paris", 1474986000, "a", "100"
2, "London", 1474986000, "a", "90"
3, "Paris", 1474986000, "b", "12"
4, "London", 1474986000, "b", "13"
5, "Paris", 1474990000, "a", "100"
6, "London", 1474990000, "a", "100"
7, "Paris", 1474990000, "a", "100"
8, "London", 1474990000, "a", "100"


I tried to check existed before insert using select statement, but i have >100000 record, it spend a lot of time for checking

any other solution can solve this problem? thanks~

3
  • 2
    Then put an index on those fields Commented Sep 28, 2016 at 15:20
  • Or write the INSERT query and process the duplicate error as an OK Commented Sep 28, 2016 at 15:21
  • Please check this docs: dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html Commented Sep 28, 2016 at 15:22

2 Answers 2

2

If there is a unique constraint on that combination of columns, assuming those columns are all declared NOT NULL...

You could create a unique index. That index would speed up a "search" for existing rows, with an appropriate SELECT statement.

 CREATE UNIQUE INDEX mytable_UX1 ON mytable (location, timestamp, type)

But if you have a UNIQUE INDEX defined, and the goal is to just to add rows that are missing, you could use INSERT IGNORE

 INSERT IGNORE INTO mytable ( ... ) VALUES ( ... ), ( ... )

An attempt to insert a row that violates a unique constraint will not throw an error. The IGNORE keyword causes MySQL to ignore more than just duplicate key violations, it turns other errors into warnings.

If you use the IGNORE keyword, errors that occur while executing the INSERT statement are ignored. For example, without IGNORE, a row that duplicates an existing UNIQUE index or PRIMARY KEY value in the table causes a duplicate-key error and the statement is aborted. With IGNORE, the row is discarded and no error occurs. Ignored errors may generate warnings instead, although duplicate-key errors do not.

Another technique I have used is to perform a check within the INSERT itself, using an INSERT ... SELECT ... with the SELECT statement including an anti-join to eliminate rows that already exist. This approach only checks for rows that already exist in the table, not "duplicates" that might be produced by the SELECT statement.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use INSERT IGNORE that keeps the script running and does not insert duplicate keys. Or you can use INSERT ... ON DUPLICATE KEY...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.