sql-execution-plan

Programatically read SQL Server's query plan suggested indexes for a specific execution of SQL?

一笑奈何 提交于 2019-12-09 11:02:56
问题 If I run this command in SSMS: set showplan_xml on GO exec some_procedure 'arg1', 'arg2','arg3' GO set showplan_xml off GO I get XML output of the full call stack involved in the query execution, as well as any suggested indexes etc. How might one read this from C#? (One use case might be to periodically enable this and log these results in a production environment to keep an eye on index suggestions.) 回答1: This is, for the most part, two separate (though related) questions. Is it possible to

Why is this an Index Scan and not a Index Seek?

左心房为你撑大大i 提交于 2019-12-08 22:57:31
问题 Here's the query: SELECT top 100 a.LocationId, b.SearchQuery, b.SearchRank FROM dbo.Locations a INNER JOIN dbo.LocationCache b ON a.LocationId = b.LocationId WHERE a.CountryId = 2 AND a.Type = 7 Location Indexes: PK_Locations: LocationId IX_Locations_CountryId_Type: CountryId, Type LocationCache Indexes: PK_LocationCache: LocationId IX_LocationCache_LocationId_SearchQuery_SearchRank: LocationId, SearchQuery, SearchRank Execution Plan: So it's doing a Index Seek on Locations, using the

How do I explain a query with parameters in MySQL

主宰稳场 提交于 2019-12-08 19:23:24
问题 I have a query SELECT foo FROM bar WHERE some_column = ? Can I get a explain plan from MySQL without filling in a value for the parameter? 回答1: So long as you're doing only an equals (and not a like, which can have short circuit affects), simply replace it with a value: EXPLAIN SELECT foo FROM bar WHERE some_column = 'foo'; Since it's not actually executing the query, the results shouldn't differ from the actual. There are some cases where this isn't true (I mentioned LIKE already). Here's an

PostgreSQL manualy change query execution plan to force using sort and sequential access instead of full scan

我与影子孤独终老i 提交于 2019-12-08 11:45:11
问题 I have simple query like this: SELECT * FROM t1 WHERE f1 > 42 AND f2 = 'foo' AND f3 = 'bar' ORDER BY f4 DESC LIMIT 10 OFFSET 100; I have index for field f4 (for other queries). Condition "f1 > 42 AND f2 = 'foo' AND f3 = 'bar'" is not representative and corresponds to 70% of records in table t1. It's about 2 000 000 records in table and it is growing up every day. Query plan explanation for this query shows using of seq scan by entire table and then performing ordering and limitation. Is it

understanding MySQL Explain output

南楼画角 提交于 2019-12-08 04:55:40
问题 I have a couple of questions regarding MySQL explain. In the first step of the evaluation, it utilizes a REF, for join type. However, upon my research on ref it states the following: All rows with matching index values are read from this table for each combination of rows from the previous tables. What is this Previous table? How can there be a previous table if its the initial step? I created an index on S.E, why does it state Using where? at the Extra column instead of Using Index ? And it

How do I optimize MySQL's queries with constants?

本秂侑毒 提交于 2019-12-08 04:43:18
问题 NOTE: the original question is moot but scan to the bottom for something relevant. I have a query I want to optimize that looks something like this: select cols from tbl where col = "some run time value" limit 1; I want to know what keys are being used but whatever I pass to explain, it is able to optimize the where clause to nothing ("Impossible WHERE noticed...") because I fed it a constant. Is there a way to tell mysql to not do constant optimizations in explain? Am I missing something? Is

SQL Server sp_ExecuteSQL and Execution Plans

﹥>﹥吖頭↗ 提交于 2019-12-07 03:25:12
问题 I have a query which is superfast in SQL Server Management STudio and super slow when run under sp_ExecuteSQL. Is this to do with caching of execution plans not happening when run under spExecuteSQL? 回答1: No. You can see both execution plans and compare them using the following query. SELECT usecounts, cacheobjtype, objtype, text, query_plan, value as set_options FROM sys.dm_exec_cached_plans CROSS APPLY sys.dm_exec_sql_text(plan_handle) CROSS APPLY sys.dm_exec_query_plan(plan_handle) cross

how to generate explain plan for entire stored procedure

最后都变了- 提交于 2019-12-07 03:11:47
问题 I usually generate explain plans using the following in sqlplus: SET AUTOTRACE ON SET TIMING ON SET TRIMSPOOL ON SET LINES 200 SPOOL filename.txt SET AUTOTRACE TRACEONLY; {query goes here} SPOOL OFF SET AUTOTRACE OFF But what If I want to generate explain plan for a stored procedure? Is there a way to generate explain plan for the entire stored procedure? The SP has no input/output parameters. 回答1: What you are generating is correctly called an "execution plan". "Explain plan" is a command

When “PARTITION LIST SUBQUERY” is in the execution plan something (a bug?) de-instantiates the package

久未见 提交于 2019-12-06 10:26:23
问题 Is this an Oracle 12c bug? I run 64-bit Oracle 12.1.0.2 on Oracle Linux. Came across a strange thing: when the execution plan switches to using "PARTITION LIST SUBQUERY" then the package used in the affected query is loosing all of its variable's values. It looks like something de-instantiates the package just like after running DBMS_SESSION.RESET_PACKAGE. The query uses a partitioned table which partitions are limited by joining with another table limited using a variable from the package

Efficient PostgreSQL query on timestamp using index or bitmap index scan?

陌路散爱 提交于 2019-12-06 07:32:09
In PostgreSQL, I have an index on a date field on my tickets table. When I compare the field against now() , the query is pretty efficient: # explain analyze select count(1) as count from tickets where updated_at > now(); QUERY PLAN --------------------------------------------------------------------------------------------------------------------------------------------- Aggregate (cost=90.64..90.66 rows=1 width=0) (actual time=33.238..33.238 rows=1 loops=1) -> Index Scan using tickets_updated_at_idx on tickets (cost=0.01..90.27 rows=74 width=0) (actual time=0.016..29.318 rows=40250 loops=1)