Data duplication Transact-SQL] [SQL Server automatically left join automatically converted into inner join, and the time of association

: Original data duplication Transact-SQL] [SQL Server automatically left join automatically converted into inner join, and the time of association

 

1, SQL Server automatically left join automatically converted into inner join question:

 

The following two statements are left join, but has transformed into a inner join


   
   
  1. drop table a,B
  2. go
  3. create table a( id int)
  4. insert into a
  5. select 1 union all
  6. select 2
  7. create table b( id int,xxx varchar( 10))
  8. insert into b
  9. select 1, 'xxx' union all
  10. select 2, 'xx'
  11. go
  12. --这个还是left join
  13. select *
  14. from a
  15. left join b
  16. on a.id = b.id and b.xxx = 'xxx'
  17. /*
  18. id id xxx
  19. 1 1 xxx
  20. 2 NULL NULL
  21. */
  22. select * --这个就是转化为inner join
  23. from a
  24. left join b
  25. on a.id = b.id
  26. where b.xxx = 'xxx'
  27. /*
  28. id id xxx
  29. 1 1 xxx
  30. */

 

The following diagram is the implementation plan:


 

2, the following statement, after running a few records will come out of it? 

 


   
   
  1. select*
  2. from
  3. (
  4. select 1 as id
  5. )a
  6. left join
  7. (
  8. select 1 as id
  9. union all
  10. select 1
  11. )b
  12. on a.id = b.id
  13. left join
  14. (
  15. select 1 as id
  16. union all
  17. select 1
  18. )c
  19. on a.id = c.id


 The reason thought about this, is because it was discovered recently wrote a report run does not always result, larger numbers, than the logic above statements statements complicated, but the problem is the same.

 

First, the query results in claim out of detailed data, since the table a correlation table b, although the table a no duplicate records, the list b has duplicate records, resulting in association recording two a record in the table a and table b, the result there are two assemblies, then the result set is then generated and then associated with a table c, c this case, since the table data would be repeated, then the final result set will be four.

Cartesian product looks the same as 2 * 2 = 4, but the fact is that table b and c table have duplicate records, a lot of duplicate data appears after the association is, the problem in time to write SQL statements, we must pay close attention.

 

If you solve this problem?

Generally separately prior data is repeated to re-table, or group by polymerization and calculated according to the demand, and then associate, so as not to cause too large numbers.

Published 416 original articles · won praise 135 · views 940 000 +

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12019961.html