|
| 1 | +.. _tutorial-long-running-queries: |
| 2 | + |
| 3 | +============================ |
| 4 | +Perform Long-Running Queries |
| 5 | +============================ |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +When MongoDB performs long-running queries using the default read |
| 16 | +concern of :readconcern:`"local"`, the snapshot time from which data is |
| 17 | +returned is subject to change. The snapshot time may move forward as the |
| 18 | +query runs, and the query may return unexpected or inconsistent results. |
| 19 | + |
| 20 | +To guarantee the snapshot time and work from a single point in time |
| 21 | +across a cluster, use read concern :readconcern:`"snapshot"`. Read |
| 22 | +concern :readconcern:`"snapshot"` allows you to specify the |
| 23 | +:ref:`atClusterTime <atClusterTime>` parameter, indicating the exact |
| 24 | +point in time from which to return your data. |
| 25 | + |
| 26 | +Example |
| 27 | +------- |
| 28 | + |
| 29 | +Create a ``movies`` collection with these documents: |
| 30 | + |
| 31 | +.. code-block:: javascript |
| 32 | + |
| 33 | + db.movies.insertMany( [ |
| 34 | + { |
| 35 | + title: 'Titanic', |
| 36 | + year: 1997, |
| 37 | + genres: [ 'Drama', 'Romance' ], |
| 38 | + rated: 'PG-13', |
| 39 | + languages: [ 'English', 'French', 'German', 'Swedish', 'Italian', 'Russian' ], |
| 40 | + released: ISODate("1997-12-19T00:00:00.000Z"), |
| 41 | + cast: [ 'Leonardo DiCaprio', 'Kate Winslet', 'Billy Zane', 'Kathy Bates' ], |
| 42 | + directors: [ 'James Cameron' ] |
| 43 | + }, |
| 44 | + { |
| 45 | + title: 'The Dark Knight', |
| 46 | + year: 2008, |
| 47 | + genres: [ 'Action', 'Crime', 'Drama' ], |
| 48 | + rated: 'PG-13', |
| 49 | + languages: [ 'English', 'Mandarin' ], |
| 50 | + released: ISODate("2008-07-18T00:00:00.000Z"), |
| 51 | + cast: [ 'Christian Bale', 'Heath Ledger', 'Aaron Eckhart', 'Michael Caine' ], |
| 52 | + directors: [ 'Christopher Nolan' ] |
| 53 | + }, |
| 54 | + { |
| 55 | + title: 'Spirited Away', |
| 56 | + year: 2001, |
| 57 | + genres: [ 'Animation', 'Adventure', 'Family' ], |
| 58 | + rated: 'PG', |
| 59 | + languages: [ 'Japanese' ], |
| 60 | + released: ISODate("2003-03-28T00:00:00.000Z"), |
| 61 | + cast: [ 'Rumi Hiiragi', 'Miyu Irino', 'Mari Natsuki', 'Takashi Naitè' ], |
| 62 | + directors: [ 'Hayao Miyazaki' ] |
| 63 | + }, |
| 64 | + { |
| 65 | + title: 'Casablanca', |
| 66 | + genres: [ 'Drama', 'Romance', 'War' ], |
| 67 | + rated: 'PG', |
| 68 | + cast: [ 'Humphrey Bogart', 'Ingrid Bergman', 'Paul Henreid', 'Claude Rains' ], |
| 69 | + languages: [ 'English', 'French', 'German', 'Italian' ], |
| 70 | + released: ISODate("1943-01-23T00:00:00.000Z"), |
| 71 | + directors: [ 'Michael Curtiz' ], |
| 72 | + lastupdated: '2015-09-04 00:22:54.600000000', |
| 73 | + year: 1942 |
| 74 | + } |
| 75 | + ] ) |
| 76 | + |
| 77 | +The following command: |
| 78 | + |
| 79 | +- Performs a :dbcommand:`find` operation on the ``movies`` collection |
| 80 | + with read concern :readconcern:`"snapshot"`. |
| 81 | + |
| 82 | +- Specifies a filter to only return documents where the ``directors`` field |
| 83 | + contains ``Christopher Nolan``. |
| 84 | + |
| 85 | +- Specifies that the operation should read data from the snapshot at |
| 86 | + cluster time ``Timestamp(1643066050, 1)``. |
| 87 | + |
| 88 | +.. important:: |
| 89 | + |
| 90 | + To run this example, you must set the :ref:`atClusterTime |
| 91 | + <atClusterTime>` value to a timestamp that occurs after the time at |
| 92 | + which you inserted the data. |
| 93 | + |
| 94 | +.. code-block:: javascript |
| 95 | + |
| 96 | + db.runCommand( { |
| 97 | + find: "movies", |
| 98 | + filter: { directors: "Christopher Nolan" }, |
| 99 | + readConcern: { |
| 100 | + level: "snapshot", |
| 101 | + atClusterTime: Timestamp(1643066050, 1) |
| 102 | + }, |
| 103 | + } ) |
| 104 | + |
| 105 | +Example output: |
| 106 | + |
| 107 | +.. code-block:: javascript |
| 108 | + |
| 109 | + { |
| 110 | + cursor: { |
| 111 | + firstBatch: [ |
| 112 | + { |
| 113 | + _id: ObjectId("61ef27ac707c25130e7611f8"), |
| 114 | + title: 'The Dark Knight', |
| 115 | + year: 2008, |
| 116 | + genres: [ 'Action', 'Crime', 'Drama' ], |
| 117 | + rated: 'PG-13', |
| 118 | + languages: [ 'English', 'Mandarin' ], |
| 119 | + released: ISODate("2008-07-18T00:00:00.000Z"), |
| 120 | + cast: [ |
| 121 | + 'Christian Bale', |
| 122 | + 'Heath Ledger', |
| 123 | + 'Aaron Eckhart', |
| 124 | + 'Michael Caine' |
| 125 | + ], |
| 126 | + directors: [ 'Christopher Nolan' ] |
| 127 | + } |
| 128 | + ], |
| 129 | + id: Long("0"), |
| 130 | + ns: 'test.movies', |
| 131 | + atClusterTime: Timestamp({ t: 1643066050, i: 1 }) |
| 132 | + }, |
| 133 | + ok: 1, |
| 134 | + '$clusterTime': { |
| 135 | + clusterTime: Timestamp({ t: 1643066060, i: 1 }), |
| 136 | + signature: { |
| 137 | + hash: Binary(Buffer.from("0000000000000000000000000000000000000000", "hex"), 0), |
| 138 | + keyId: Long("0") |
| 139 | + } |
| 140 | + }, |
| 141 | + operationTime: Timestamp({ t: 1643066060, i: 1 }) |
| 142 | + } |
| 143 | + |
| 144 | +Consideration |
| 145 | +~~~~~~~~~~~~~ |
| 146 | + |
| 147 | +Due to the small size of the data set used in this example, there is not |
| 148 | +a large benefit to using read concern :readconcern:`"snapshot"`. The |
| 149 | +query should run fast enough such that all data is returned at the |
| 150 | +same cluster time without needing to specify read concern |
| 151 | +:readconcern:`"snapshot"`. |
| 152 | + |
| 153 | +The purpose of the preceding example is to demonstrate the syntax and expected |
| 154 | +output for read concern :readconcern:`"snapshot"`. |
| 155 | + |
| 156 | +Configure Snapshot Retention |
| 157 | +---------------------------- |
| 158 | + |
| 159 | +By default, the WiredTiger storage engine retains snapshot history for |
| 160 | +300 seconds. This means that you can specify an :ref:`atClusterTime |
| 161 | +<atClusterTime>` value for your query that is up to 300 seconds before |
| 162 | +the time at which you run the query. |
| 163 | + |
| 164 | +If your query runs for longer than 300 seconds, you may wish to increase |
| 165 | +the snapshot retention period. To specify how long WiredTiger keeps the |
| 166 | +snapshot history, modify the |
| 167 | +:parameter:`minSnapshotHistoryWindowInSeconds` parameter. |
| 168 | + |
| 169 | +Increasing the value of :parameter:`minSnapshotHistoryWindowInSeconds` |
| 170 | +increases disk usage because the server must maintain the history of |
| 171 | +older modified values within the specified time window. The amount of |
| 172 | +disk space used depends on your workload, with higher volume workloads |
| 173 | +requiring more disk space. |
0 commit comments