WEBVTT

1
00:00:00.000 --> 00:00:04.067
A shopper opens order history and asks for twenty recent orders.

2
00:00:04.067 --> 00:00:07.667
Why can that small request create so much database work?

3
00:00:07.667 --> 00:00:16.267
We used Navicat Premium eighteen to inspect three query problems in PostgreSQL, on a research
table with one million synthetic orders.

4
00:00:16.267 --> 00:00:18.867
The application scenario is illustrative.

5
00:00:18.867 --> 00:00:21.667
A shopper opens order history and filters by date.

6
00:00:21.667 --> 00:00:24.733
The application needs the latest twenty matching orders.

7
00:00:24.733 --> 00:00:29.800
We start with the same customer, date window, and covering index throughout the comparison.

8
00:00:29.800 --> 00:00:35.133
The original SQL converts the creation timestamp to a U T C date before testing both boundaries.

9
00:00:35.133 --> 00:00:41.533
In Navicat, the plan shows the customer in the index condition, with the date expression left as a
separate filter.

10
00:00:41.533 --> 00:00:42.867
That distinction matters.

11
00:00:42.867 --> 00:00:48.467
The index can find this customer’s entries, but the database evaluates the date expression as a filter.

12
00:00:48.467 --> 00:00:52.467
In this illustration, matching orders continue and other orders drop out.

13
00:00:52.467 --> 00:00:57.267
The rewrite compares the timestamp directly against the start and end of the U T C window.

14
00:00:57.267 --> 00:00:59.333
Include the start and exclude the end.

15
00:00:59.333 --> 00:01:03.400
The customer, selected fields, order, and twenty-row limit remain identical.

16
00:01:03.400 --> 00:01:07.400
Now, in Navicat, both timestamp boundaries appear in the index condition.

17
00:01:07.400 --> 00:01:09.867
Postgres can start inside the relevant range.

18
00:01:09.867 --> 00:01:14.533
The original and rewritten queries returned the same twenty records in the same order.

19
00:01:14.533 --> 00:01:20.000
Five alternating native measurement pairs gave medians of about eight and seven hundredths of a
millisecond.

20
00:01:20.000 --> 00:01:21.133
The ranges overlapped.

21
00:01:21.133 --> 00:01:25.200
The verified result is the move from a separate filter into the index condition.

22
00:01:25.200 --> 00:01:27.133
Navicat makes that change easy to see.

23
00:01:27.133 --> 00:01:30.067
This query selects one customer and a date window.

24
00:01:30.067 --> 00:01:36.133
It sorts newest first, with order I D breaking ties, and returns only twenty records.

25
00:01:36.133 --> 00:01:40.333
Navicat’s Visual Explain shows what happens without a suitable covering index.

26
00:01:40.333 --> 00:01:43.533
PostgreSQL scans the table in parallel and filters the rows.

27
00:01:43.533 --> 00:01:49.333
Each worker sorts its matches, and the database merges the ordered streams before returning the
first twenty.

28
00:01:49.333 --> 00:01:51.733
The index is designed around the query.

29
00:01:51.733 --> 00:01:54.267
The customer key narrows the search.

30
00:01:54.267 --> 00:01:57.000
Timestamp and order I D provide the order.

31
00:01:57.000 --> 00:02:00.333
The included fields supply status and total amount.

32
00:02:00.333 --> 00:02:05.733
With the same query, Visual Explain now shows an index-only scan feeding the limit directly.

33
00:02:05.733 --> 00:02:09.600
The analyzed run confirms that PostgreSQL needed no heap fetches.

34
00:02:09.600 --> 00:02:17.333
In these single runs, execution dropped from about two hundred and seventy-nine milliseconds to
under half a millisecond.

35
00:02:17.333 --> 00:02:20.933
Shared buffer hits fell from over nine thousand to four.

36
00:02:20.933 --> 00:02:23.200
All twenty ordered records matched.

37
00:02:23.200 --> 00:02:25.733
The index changed how PostgreSQL ran the query.

38
00:02:25.733 --> 00:02:32.000
Navicat helped us identify the extra work, see why the plan changed, and verify the result.

39
00:02:32.000 --> 00:02:35.000
A shopper wants to continue through a long order history.

40
00:02:35.000 --> 00:02:41.467
The application requests twenty rows, but what the database must read depends on how the
application asks for the next page.

41
00:02:41.467 --> 00:02:45.067
Here, the original query requests an offset of one hundred thousand.

42
00:02:45.067 --> 00:02:48.600
The estimated plan in Navicat uses an index-only scan and a limit.

43
00:02:48.600 --> 00:02:53.000
The analyzed run shows that the scan emitted one hundred thousand and twenty rows.

44
00:02:53.000 --> 00:02:55.067
Think of the index as an ordered stream.

45
00:02:55.067 --> 00:02:59.667
Postgres advances through the preceding rows and the limit operator discards the offset.

46
00:02:59.667 --> 00:03:02.133
Only the next twenty rows reach the application.

47
00:03:02.133 --> 00:03:04.867
Keyset pagination gives the database a starting point.

48
00:03:04.867 --> 00:03:08.067
The cursor contains the previous page’s timestamp and order I D.

49
00:03:08.067 --> 00:03:11.200
Their combination keeps the order stable when timestamps tie.

50
00:03:11.200 --> 00:03:13.200
The query compares both values together.

51
00:03:13.200 --> 00:03:15.600
The keyset plan also uses an index-only scan.

52
00:03:15.600 --> 00:03:20.400
This time the cursor appears in the index condition, and the scan emits only twenty rows.

53
00:03:20.400 --> 00:03:25.000
We verified that both queries return the same twenty full records, in the same order.

54
00:03:25.000 --> 00:03:32.000
In these single runs, server execution fell from about nine hundred and seventy-seven milliseconds
to about sixteen.

55
00:03:32.000 --> 00:03:36.400
Finding the cursor separately took about one hundred and eleven milliseconds.

56
00:03:36.400 --> 00:03:40.867
Keyset works best when the previous page already supplies that cursor.

57
00:03:40.867 --> 00:03:43.467
Navicat helps us see the work that changed.

58
00:03:43.467 --> 00:03:47.600
The date rewrite moved both bounds into the index condition.

59
00:03:47.600 --> 00:03:49.600
The timing ranges overlapped.

60
00:03:49.600 --> 00:03:51.867
The covering index removed the extra sort.

61
00:03:51.867 --> 00:03:57.133
Keyset pagination reduced scan output, with cursor discovery measured separately.

62
00:03:57.133 --> 00:04:00.933
PostgreSQL executed the optimized queries.

63
00:04:00.933 --> 00:04:04.200
Navicat helped diagnose and verify the changes.

64
00:04:04.200 --> 00:04:11.333
Navicat provided complimentary evaluation access and offered an annual subscription associated
with publication.

65
00:04:11.333 --> 00:04:14.067
This is an independent database evaluation.

66
00:04:14.067 --> 00:04:17.400
No Java or Spring Boot application benchmark was run.
