简体   繁体   中英

mySQL: Most efficient method to update records in large DB?

Say I have a table with 1000s of records and I only want to update one record. Does it make the query faster if I specify more 'WHERE' clauses to narrow down the search to fewer possible matching records, or is it faster to just state one WHERE clause (eg. recordID)?

EXAMPLE:

UPDATE table
SET record_name = 'new name'
WHERE record_ID = 'x'
LIMIT 1

or

UPDATE table
SET record_name = 'new name'
WHERE record_ID = 'foo'
AND record_city = 'bah'
LIMIT 1

假设record_ID是主键,这绝对是更新单个记录的最快方法。

if record_ID is primary key, the first solution is the fastest. I think that it is not necesary to put "LIMIT 1" because it is implicit in the primary key

It's worth noting that the two commands actually are not functionally equivalent as well. You should always run the query, which does what you want to do.

The optimizer in the database will figure it out. In this case, even if you specify both, it will have to access the location of the row to delete it, reading an extra WHERE against a column is a very minor penalty compared to navigating to the row in the first place.

Note also that using LIMIT without an ORDER BY will break serialization for replication and restoration of binary logs for point in time recovery in STATEMENT mode.

It's in this case better to specify in the WHERE all that is needed and not need the LIMIT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM