sql-execution-plan

SQL Server pick random (or first) value with aggregation

谁说胖子不能爱 提交于 2019-11-28 05:24:12
问题 How can I get SQL Server to return the first value (any one, I don't care, it just needs to be fast) it comes across when aggregating? For example, let's say I have: ID Group 1 A 2 A 3 A 4 B 5 B and I need to get any one of the ID's for each group. I can do this as follows: Select max(id) ,group from Table group by group which returns ID Group 3 A 5 B That does the job, but it seems stupid to me to ask SQL Server to calculate the highest ID when all it really needs to do is to pick the first

Sql wildcard: performance overhead?

冷暖自知 提交于 2019-11-28 05:04:21
问题 I've Googled this question and can't seem to find a consistent opinion, or many opinions that are based on solid data. I simply would like to know if using the wildcard in a SQL SELECT statement incurs additional overhead than calling each item out individually. I have compared the execution plans of both in several different test queries, and it seems that the estimates always read the same. Is it possible that some overhead is incurred elsewhere, or are they truly handled identically? What

What is a “Bitmap heap scan” in a query plan?

被刻印的时光 ゝ 提交于 2019-11-28 03:44:11
I want to know the principle of "Bitmap heap scan", I know this often happens when I execute a query with OR in the condition. Who can explain the principle behind a "Bitmap heap scan"? The best explanation comes from Tom Lane , which is the algorithm's author unless I'm mistaking. See also the wikipedia article . In short, it's a bit like a seq scan. The difference is that, rather than visiting every disk page, a bitmap index scan ANDs and ORs applicable indexes together, and only visits the disk pages that it needs to. This is different from an index scan, where the index is visited row by

Faster way to delete matching rows?

倖福魔咒の 提交于 2019-11-28 03:39:02
I'm a relative novice when it comes to databases. We are using MySQL and I'm currently trying to speed up a SQL statement that seems to take a while to run. I looked around on SO for a similar question but didn't find one. The goal is to remove all the rows in table A that have a matching id in table B. I'm currently doing the following: DELETE FROM a WHERE EXISTS (SELECT b.id FROM b WHERE b.id = a.id); There are approximately 100K rows in table a and about 22K rows in table b. The column 'id' is the PK for both tables. This statement takes about 3 minutes to run on my test box - Pentium D, XP

Understanding the results of Execute Explain Plan in Oracle SQL Developer

落爺英雄遲暮 提交于 2019-11-28 02:59:01
I'm trying to optimize a query but don't quite understand some of the information returned from Explain Plan . Can anyone tell me the significance of the OPTIONS and COST columns? In the OPTIONS column, I only see the word FULL. In the COST column, I can deduce that a lower cost means a faster query. But what exactly does the cost value represent and what is an acceptable threshold? Jeffrey Kemp The output of EXPLAIN PLAN is a debug output from Oracle's query optimiser. The COST is the final output of the Cost-based optimiser (CBO), the purpose of which is to select which of the many different

Performance of SQL “EXISTS” usage variants

ⅰ亾dé卋堺 提交于 2019-11-27 19:22:48
Is there any difference in the performance of the following three SQL statements? SELECT * FROM tableA WHERE EXISTS (SELECT * FROM tableB WHERE tableA.x = tableB.y) SELECT * FROM tableA WHERE EXISTS (SELECT y FROM tableB WHERE tableA.x = tableB.y) SELECT * FROM tableA WHERE EXISTS (SELECT 1 FROM tableB WHERE tableA.x = tableB.y) They all should work and return the same result set. But does it matter if the inner SELECT selects all fields of tableB, one field, or just a constant? Is there any best practice when all statements behave equal? The truth about the EXISTS clause is that the SELECT

SQL Server Plans : difference between Index Scan / Index Seek

和自甴很熟 提交于 2019-11-27 17:11:53
In a SQL Server Execution plan what is the difference between an Index Scan and an Index Seek I'm on SQL Server 2005. Justin An index scan is where SQL server reads the whole of the index looking for matches - the time this takes is proportional to the size of the index. An index seek is where SQL server uses the b-tree structure of the index to seek directly to matching records (see http://mattfleming.com/node/192 for an idea on how this works) - time taken is only proportional to the number of matching records. In general an index seek is preferable to an index scan (when the number of

How do you interpret a query's explain plan?

冷暖自知 提交于 2019-11-27 16:54:26
When attempting to understand how a SQL statement is executing, it is sometimes recommended to look at the explain plan. What is the process one should go through in interpreting (making sense) of an explain plan? What should stand out as, "Oh, this is working splendidly?" versus "Oh no, that's not right." David Aldridge I shudder whenever I see comments that full tablescans are bad and index access is good. Full table scans, index range scans, fast full index scans, nested loops, merge join, hash joins etc. are simply access mechanisms that must be understood by the analyst and combined with

Do foreign key constraints influence query transformations in Oracle?

删除回忆录丶 提交于 2019-11-27 16:12:44
问题 I have a situation like this: create table a( a_id number(38) not null, constraint pk_a primary key (id) ); create table b( a_id number(38) not null ); create index b_a_id_index on b(a_id); Now b.a_id is in fact meant to be a foreign key referencing a.a_id , but it isn't formally declared as such. Obviously, it should be for integrity reasons. But does a foreign key constraint also improve join performance in general or in specific cases? If yes, for what types of query transformations? Is

Shredding XML From Execution Plans

陌路散爱 提交于 2019-11-27 15:50:41
I'll preface this by saying that I hate XML, horrible stuff to work with, but necessary sometimes. My current issue is that I'm trying to take the XML from an execution plan (supplied by a user, so could be any size) and shred this into a table for further manipulation. I'm down to two options at the moment; I could work out the maximum amount of nodes available for an execution plan (I suspect this would be a lot) and create the whole script that could be used for any XML input. This would be a one time thing so not an issue. The alternative would be to work out the number of nodes