1

次のようなクエリがあります。

-- MySQL Query
SELECT * FROM table1 WHERE start_date >= CURRENT_TIMESTAMP - INTERVAL 7 DAY

-- MSSQL Query
SELECT * FROM table1 WHERE start_date >= GETDATE() - 7

右側の式は列の値に依存しないため、クエリオプティマイザーが次のような定数値に最適化することを期待する必要があります。

SELECT * FROM table1 WHERE start_date >= '2012-04-28 14:54:31'

または、定数を計算し、コードを使用してクエリを作成する必要があります。

4

2 に答える 2

2

According to the documentation for MySQL's NOW() function (for which CURRENT_TIMESTAMP is a synonym):

NOW() returns a constant time that indicates the time at which the statement began to execute. (Within a stored function or trigger, NOW() returns the time at which the function or triggering statement began to execute.) This differs from the behavior for SYSDATE(), which returns the exact time at which it executes.

As such, the query optimiser will treat it as a constant, as desired. One can see this in the EXPLAIN output.

I can't speak for MSSQL, although perhaps this blog is worth a read?

于 2012-05-05T10:23:37.023 に答える
-1

定数ではなく、右側に関数があります。クエリは毎回再評価する必要があります...

(私はポイントを逃しますか??)

編集

@Martinと@eggyvalが親切に説明したように、私は本当にポイントを逃しました. 質問は明らかにコンパイル時の最適化に関するものではないので、私のようなジャークに何が起こるべきかの記念碑として、この貧弱で貧弱な発言を残します...

于 2012-05-05T10:15:55.830 に答える