SQL Server
92
0

SQL Server: LEFT JOIN vs NOT EXISTS (Performance)

[et_pb_section fb_built=”1″ _builder_version=”3.22″][et_pb_row _builder_version=”3.25″ background_size=”initial” background_position=”top_left” background_repeat=”repeat”][et_pb_column type=”4_4″ _builder_version=”3.25″ custom_padding=”|||” custom_padding__hover=”|||”][et_pb_text _builder_version=”4.6.5″ background_size=”initial” background_position=”top_left” background_repeat=”repeat” hover_enabled=”0″ text_text_color=”#000000″ sticky_enabled=”0″]
En mis años como consultor he visto en muchas ocasiones que los programadores usan los JOIN para hacer algún tipo de búsqueda del tipo: Traer los clientes que no tienen ventas o cosas similares.


El JOIN como concepto esta mas pensado para buscar datos entre tablas y presentarlos, por ejemplo si tenemos una tabla facturas y otra clientes entonces es lógico que si necesitamos datos de clientes necesitemos hacer algún tipo de JOIN


En este post veremos el impacto de performance que tiene usar un LEFT JOIN para buscar por ejemplo los clientes que no tienen ordenes en lugar de usar un NOT EXISTS.


Veamos el siguiente ejemplo donde las dos consultas van a retornar los mismos resultados.

[/et_pb_text][et_pb_dmb_code_snippet code=”VVNFIEFEVkVOVFVSRVdPUktTMjAxNyAKR08KClNFTEVDVCAqIEZST00gUFJPRFVDVElPTi5QUk9EVUNUIFAKV0hFUkUgTk9UIEVYSVNUUyAKKAogU0VMRUNUICogRlJPTSBTQUxFUy5TQUxFU09SREVSREVUQUlMIEQKIFdIRVJFIFAuUFJPRFVDVElEID0gRC5QUk9EVUNUSUQgCikKClNFTEVDVCBQLiogRlJPTSBQUk9EVUNUSU9OLlBST0RVQ1QgUApMRUZUIEpPSU4gU0FMRVMuU0FMRVNPUkRFUkRFVEFJTCBECk9OIFAuUFJPRFVDVElEID0gRC5QUk9EVUNUSUQgCldIRVJFIEQuUFJPRFVDVElEIElTIE5VTEw=” _builder_version=”4.0.9″]VVNFIEFEVkVOVFVSRVdPUktTMjAxNyAKR08KClNFTEVDVCAqIEZST00gUFJPRFVDVElPTi5QUk9EVUNUIFAKV0hFUkUgTk9UIEVYSVNUUyAKKAogU0VMRUNUICogRlJPTSBTQUxFUy5TQUxFU09SREVSREVUQUlMIEQKIFdIRVJFIFAuUFJPRFVDVElEID0gRC5QUk9EVUNUSUQgCikKClNFTEVDVCBQLiogRlJPTSBQUk9EVUNUSU9OLlBST0RVQ1QgUApMRUZUIEpPSU4gU0FMRVMuU0FMRVNPUkRFUkRFVEFJTCBECk9OIFAuUFJPRFVDVElEID0gRC5QUk9EVUNUSUQgCldIRVJFIEQuUFJPRFVDVElEIElTIE5VTEw=[/et_pb_dmb_code_snippet][et_pb_text _builder_version=”4.6.5″ hover_enabled=”0″ text_text_color=”#000000″ sticky_enabled=”0″]En ambas consultas vamos a observar que los resultados son idénticos, retornando 238 registros.

Ahora bien vamos a ver que sucede con sus respectivos planes de ejecución [/et_pb_text][et_pb_image src=”https://blogs.triggerdb.com/wp-content/uploads/2019/12/left_vs_exists_01.png” _builder_version=”4.0.9″][/et_pb_image][et_pb_image src=”https://blogs.triggerdb.com/wp-content/uploads/2019/12/left_vs_exists_02.png” _builder_version=”4.0.9″][/et_pb_image][et_pb_text _builder_version=”4.6.5″ custom_padding=”0px|||||” hover_enabled=”0″ text_text_color=”#000000″ sticky_enabled=”0″]Podemos observar que en el caso del NOT EXISTS hay una operación eficiente en del índice pero en el LEFT JOIN vemos un SCAN
El costo del primer plan es de 0.10 y el segundo de 0.68 (casi 7 veces mas)
[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Tags: ,

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed