MCPcopy Index your code
hub / github.com/MagicStack/asyncpg / explain

Method explain

asyncpg/prepared_stmt.py:126–165  ·  view source on GitHub ↗

Return the execution plan of the statement. :param args: Query arguments. :param analyze: If ``True``, the statement will be executed and the run time statitics added to the return value. :return: An object representing the execution plan. This valu

(self, *args, analyze=False)

Source from the content-addressed store, hash-verified

124
125 @connresource.guarded
126 async def explain(self, *args, analyze=False):
127 """Return the execution plan of the statement.
128
129 :param args: Query arguments.
130 :param analyze: If ``True``, the statement will be executed and
131 the run time statitics added to the return value.
132
133 :return: An object representing the execution plan. This value
134 is actually a deserialized JSON output of the SQL
135 ``EXPLAIN`` command.
136 """
137 query = 'EXPLAIN (FORMAT JSON, VERBOSE'
138 if analyze:
139 query += ', ANALYZE) '
140 else:
141 query += ') '
142 query += self._state.query
143
144 if analyze:
145 # From PostgreSQL docs:
146 # Important: Keep in mind that the statement is actually
147 # executed when the ANALYZE option is used. Although EXPLAIN
148 # will discard any output that a SELECT would return, other
149 # side effects of the statement will happen as usual. If you
150 # wish to use EXPLAIN ANALYZE on an INSERT, UPDATE, DELETE,
151 # MERGE, CREATE TABLE AS, or EXECUTE statement without letting
152 # the command affect your data, use this approach:
153 # BEGIN;
154 # EXPLAIN ANALYZE ...;
155 # ROLLBACK;
156 tr = self._connection.transaction()
157 await tr.start()
158 try:
159 data = await self._connection.fetchval(query, *args)
160 finally:
161 await tr.rollback()
162 else:
163 data = await self._connection.fetchval(query, *args)
164
165 return json.loads(data)
166
167 @connresource.guarded
168 async def fetch(self, *args, timeout=None):

Callers 1

Calls 4

transactionMethod · 0.80
rollbackMethod · 0.80
startMethod · 0.45
fetchvalMethod · 0.45

Tested by 1