🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: cn.bbs.comp.database
Article #602

һ��򵥵�SQL��ѯ���ı���... zz

#602
From: �F��ħ��ɳ
Date: Fri, 21 Nov 2008 06:22
121 lines
6570 bytes
http://www.cnblogs.com/Mainz/archive/2008/07/31/1257000.html

��������SQL Server Management Studio�IJ�ѯ�������룺 SELECT * FROM t1 WHERE 
c1=2; 

��������ʲô����ݿ���ôȥִ�в�ѯ�ģ� -  ֪��Ȼ��֪������Ȼ��

 

1.         Query Parsing, Synatax Error Check, and Authorization: Query 
Processer check the user is authorized to run the query;����ѯ���������Ƚ���
�﷨�������﷨��飬�������Ȩ�����û�л����Ȩִ����صIJ����������﷨����
�������?��

2.         Query Re-writing��Semantic Optimization�� ����ѯ����������дSQL��
�䲢����Ӧ���Ż�����

���磺SELECT Emp.name, Emp.salary
                         FROM Emp, Dept
                            WHERE Emp.deptno = Dept.dno 

              Join�Ƕ���ģ���ѯ����������д����䲢ɾ���Dept��Join��

3.         Compile the SQL into internal query Plan; ������SQL���ڲ���ѯ�ƻ�
��

       ��ݿ��ѡ����ѵķ������������������ȫ��ɨ�裬����ʹ������(pai
rs of key and location���������Ŀ¼)����ݿ��Ƚ����ǵijɱ������γ��ڲ�ִ��
�ƻ�����ݿ��ڲ��������ѡ�����ִ�мƻ���������Ż�����Optimizer����

4.         The query plan is handled by plan executor, which consists of 
many operators, e.g. joining, sorting, grouping... ����ѯ�ƻ����ƻ�ִ����ִ��
���ƻ�ִ������ܶ�����ߣ�����ִ��Join, Sort, Group�ȣ���Ҫ�з��ʷ���������
��������ȡ��Щ���ҳ������ҳ������ֱ�ӻ������У���Щ�л������������... ...��


5.         Transaction manager is started as well to ensure the ACID of 
operations. �����������ͬʱ��������֤ԭ�Ӳ����IJ���һ����, ���������������
��������������й�������ҳ���ļ����������������������־�������͵�Ҳ��ִ�У�
�����в�ѯִ����һ�˰�����С���

 

ִ��������䣺SELECT * FROM t1 WHERE c1=2�ļ򵥹�̣�

 

1.        ɨ��t1�?����ǿ�ʼ��ȡ��һ����¼������ȡ��һ����¼�����������
���һ����¼����ô���4����

2.        ����¼�Ƿ�����WHERE��������������3��������ص���1����

3.        �Ѽ�¼�ӵ����

4.        ���ؽ���ͻ��ˡ�

 

 

�����������Ż������ܱȽ�ȫ��ɨ���������ܾ���ִ�мƻ�Ϊɨ��������
�������ƣ������1����ɣ�

ɨ������(Clustered or no-clustered)����λ����¼��

 

���磺

 SELECT * FROM Customers WHERE contactID IN

(SELECT contactID FROM Contacts WHERE contactID=86) 

ORDER BY CustomerName DESC; 

 

���ӵIJ����У�

1.        �ڱ�[Contacts]��ɨ������λ��¼��

2.        �ڱ�[Customers]��ɨ������λ��¼��

3.        Hash match (inner join)����ϣƥ�䣬���ӡ�

4.        Sort������

 

�й�Join

SQL Server employs three types of join operations: 

Nested loops joins 
Merge joins 
Hash joins 
 

���Join�������С������С��10�У�Ȼ�������Join����ܴ��������������ϣ���N
ested loops joins�����ġ���ԭ��ο�Understanding Nested Loops Joins�� 
�������Join���붼��С��������������������������ɨ�������������õ� 
scanning sorted indexes������Merge joins�����ġ���ԭ��ο�Understanding 
Merge Joins�� 
Hash joins������Ч�Ĵ�������ġ�û������ġ�û����������롣����Ը��Ӳ�ѯ��
�м��������Ч�������ο�Understanding Hash Joins�� 
 

��Щֻ��һЩ�򵥵Ĺ�̣���ʵ��ݿ��ڲ�ִ�й�����൱���ӵġ���Ȼ���Dz������
�⿪����Ա�����˽������ε�֪ʶ�DZ���ģ��ܹ����������ǵĿ�����Ӧ�á���������
Ҫ֪����ô�ã�����Ҫ֪��Ϊʲô������֪��Ȼ��֪������Ȼ�������һ����ݿ���
���򵥽ṹͼ������Ȥ���˲ο���

 

http://images.cnblogs.com/cnblogs_com/mainz/DBMS.PNG 
--
��ͨ���\ħ����Ů����ɫ�ޥ����`���ѩ`��
����ޥ����`���ѩ`���� ħ�h���ե����ʥ륹�ѩ`����
ħ��ɳ ��������˽? �������ʡ�������Ρ���Ů������

�� ��Դ:����ˮ˼Դ bbs.sjtu.edu.cn��[FROM: 207.46.55.28]
Technical Details
Message-ID: <4d93jl$bCj@bbs.sjtu.edu.cn>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.isoc.lu!news.shec6.edu.cn!SJTU

Show Full Headers
Complete Article Headers
X-Google-Thread: 101948,6a437f05b07dde90,start
X-Google-Attributes: gid101948,domainid0,public,usenet
X-Google-Language: Chinese,GB
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!news.isoc.lu!news.shec6.edu.cn!SJTU
From: Marisa.bbs@bbs.sjtu.edu.cn (�F��ħ��ɳ)
Newsgroups: cn.bbs.comp.database
Subject: һ��򵥵�SQL��ѯ���ı���... zz
Date: 21 Nov 2008 06:22:41 GMT
Organization: ��ˮ˼Դ
Message-ID: <4d93jl$bCj@bbs.sjtu.edu.cn>
Mime-Version: 1.0
Content-Type: text/plain; charset="gb2312"
Content-Transfer-Encoding: 8bit
X-Filename: database/M.1227248543.A