大家都用mysql知道:mysql的SQL_CALC_FOUND_ROWS在postgresql中是否有類似的函數?
SELECT SQL_CALC_FOUND_ROWS ..... FROM table WHERE ... LIMIT 5, 10;
,右運行此之後:
SELECT FOUND_ROWS();
我如何做到這一點的postrgesql?到目前爲止,我發現只有我必須發送查詢兩次的方法...
大家都用mysql知道:mysql的SQL_CALC_FOUND_ROWS在postgresql中是否有類似的函數?
SELECT SQL_CALC_FOUND_ROWS ..... FROM table WHERE ... LIMIT 5, 10;
,右運行此之後:
SELECT FOUND_ROWS();
我如何做到這一點的postrgesql?到目前爲止,我發現只有我必須發送查詢兩次的方法...
不,沒有(至少not as of July 2007)。恐怕你不得不訴諸:
BEGIN ISOLATION LEVEL SERIALIZABLE;
SELECT id, username, title, date FROM posts ORDER BY date DESC LIMIT 20;
SELECT count(id, username, title, date) AS total FROM posts;
END;
的isolation level需要爲SERIALIZABLE
,以確保查詢沒有看到SELECT語句之間的併發更新。
但是,您擁有的另一選項是使用觸發器對行進行計數,因爲它們是INSERT
ed或DELETE
d。假設你有如下表:
CREATE TABLE posts (
id SERIAL PRIMARY KEY,
poster TEXT,
title TEXT,
time TIMESTAMPTZ DEFAULT now()
);
INSERT INTO posts (poster, title) VALUES ('Alice', 'Post 1');
INSERT INTO posts (poster, title) VALUES ('Bob', 'Post 2');
INSERT INTO posts (poster, title) VALUES ('Charlie', 'Post 3');
然後,執行以下步驟來創建一個名爲POST_COUNT表包含posts
行數的運行計數:
-- Don't let any new posts be added while we're setting up the counter.
BEGIN;
LOCK TABLE posts;
-- Create and initialize our post_count table.
SELECT count(*) INTO TABLE post_count FROM posts;
-- Create the trigger function.
CREATE FUNCTION post_added_or_removed() RETURNS TRIGGER AS $$
BEGIN
IF TG_OP = 'DELETE' THEN
UPDATE post_count SET count = count - 1;
ELSIF TG_OP = 'INSERT' THEN
UPDATE post_count SET count = count + 1;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
-- Call the trigger function any time a row is inserted.
CREATE TRIGGER post_added_or_removed_tgr
AFTER INSERT OR DELETE
ON posts
FOR EACH ROW
EXECUTE PROCEDURE post_added_or_removed();
COMMIT;
注意這個保持運行計數爲,全部爲,行數爲posts
。爲了保持一定行的運行計數,你必須調整它:
SELECT count(*) INTO TABLE post_count FROM posts WHERE poster <> 'Bob';
CREATE FUNCTION post_added_or_removed() RETURNS TRIGGER AS $$
BEGIN
-- The IF statements are nested because OR does not short circuit.
IF TG_OP = 'DELETE' THEN
IF OLD.poster <> 'Bob' THEN
UPDATE post_count SET count = count - 1;
END IF;
ELSIF TG_OP = 'INSERT' THEN
IF NEW.poster <> 'Bob' THEN
UPDATE post_count SET count = count + 1;
END IF;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;
不,PostgreSQL並不試圖計算所有相關結果時,你只需要10個結果。您需要一個單獨的COUNT來計算所有結果。
有一個簡單的方法,但請記住,以下COUNT(*)匯聚功能將應用於所有行回來後,在那裏和限制之前/偏移量(可能是costy)
選擇 ID, 「計數」(*)OVER()AS CNT FROM 對象 WHERE ID> 2 OFFSET 50 LIMIT 5
好,感謝您的! – helle 2010-07-31 19:23:30