2

I am using mysql database, and now I must make sure that a value in a column should be 1 while it may be 1 already. So, considering the following two statements:

UPDATE category SET is_leaf=1 WHERE id=9

or

UPDATE category SET is_leaf=1 WHERE id=9 AND is_leaf=0

id is the primary key, the difference is when is_leaf is already 1, then to update it or not, which is more efficient?

I know it doesn't matter a lot, but I want to find out to better understanding mysql.

4

2 に答える 2

2
UPDATE category SET is_leaf=1 WHERE id=9 AND is_leaf=0

is more efficient. because then it update only relevant data. otherwise even if the is_leaf=1 records are also going to update.

Assume that there are 1000 record on the table and it take 1s to update one record. if u trying to update all records then it will take 1000 S. but assume in this scenario there are is_leaf=0 record count is 150 then if you use this second statement it will take only 150 seconds instead of 1000 s.

Edit :

Queries With Search Arguments (SARGs)

A WHERE clause helps you to restrict the number of rows returned by a query. However, the manner in which the WHERE condition is specified can impact the performance of the query. If the WHERE condition is written such that it uses a function that takes an indexed column as the input, then the index is ignored and the entire table is scanned. This results in performance degradation. For example, the following results in a table scan because the column OrderDate is used in a function:

SELECT CustomerID, EmployeeID FROM Orders
WHERE DATEDIFF(m, OrderDate, GetDate())>3

If the function is rewritten as shown below, then the query seeks the required value using an index and this improves performance:

SELECT CustomerID, EmployeeID FROM Orders
WHERE OrderDate < DATEADD(m, -3, GetDate())

The filter criteria in the second query is said to use a Searchable Argument or SARG because the query optimizer can use an index seek operation during execution.

for more information about this you better read improving query performance

and also read this for Speeding up Searches and Filters

于 2013-01-24T05:01:18.140 に答える
1

AND is_leaf=0含まれているクエリは、より効率的な場合があります。

主キーに基づいて行を検索する場合、大きな違いはありません。(インデックスの可用性は on (id,is_leaf)わずかな違いを生む可能性があります。)しかし、MySQLが更新する行がないことを認識するとすぐに、より短いコードパスを取ることができます。

ただし、その述語がない場合、MySQLは行を見つけ、行ロックを取得し(InnoDBの場合)、「BEFORE UPDATEFOREACHROW」トリガーを起動する必要があります。次に、MySQLは列の値が実際に変更されているかどうかを確認する必要があります(トリガーの実行により、1つ以上の列が異なる値に設定されている可能性があることに注意してください)。MySQLが行に変更がないことを検出した場合、「ON UPDATE」タイムスタンプ列の設定をスキップし、「AFTER UPDATE FOR EACH ROW」トリガーを起動して、影響を受ける行数をゼロに設定できます。(トランザクションのコンテキストでは、行が変更されていないと判断した後、MySQLが行ロックを解放できるかどうか、またはコミットまたはロールバックまで行ロックが保持され続けるかどうかは明確ではありません。)

したがって、2つのステートメントの大きな違いの1つは、行に実際の変更がない場合でも、MySQLが「FOREACHROW」トリガーを起動することです。ただし、WHERE句で除外された行に対して「FOREACHROW」トリガーは発生しません。

単純なケースでは、トリガーがない場合、パフォーマンスに測定可能な違いはないと思います。

私の個人的な好みは、追加の述語を含めることです。これにより、(InnoDB)インテント行ロックが要求または保持されたり、FOREACHROWトリガーが起動されたりすることがなくなります。


また、行のロックとトリガーの実行を除けば、これら2つのステートメントがまったく同じである限り、実際にはそうではありません。is_leaf少なくとも、NULLまたは0または1以外の値を含む可能性がある一般的な場合はそうではありません。

このステートメントを考えると:

UPDATE category SET is_leaf=1 WHERE id=9

まだ1に等しくない場合は常に1に設定される同等のステートメントの場合、is_leaf実際には、NULLおよび1以外の値をチェックする必要があります。たとえば、次のようになります。

UPDATE category SET is_leaf=1 WHERE id=9 AND NOT (is_leaf <=> 1)

is_leafたとえば、次のステートメントを使用して、がNULLまたは2の場合に何が起こるかを考えてみます。

UPDATE category SET is_leaf=1 WHERE id=9 AND is_leaf=0
于 2013-01-24T05:27:12.933 に答える