33.6. �����봥����֮�Ƚ�

����ô��������Ըɵ�����ͬ��Ҳ������ PostgreSQL ����ϵͳ����ɡ� Ŀǰ�����ù�����ʵ�ֵĶ���֮һ��ijЩԼ�����ر�������� ���ǿ�����ij�ֶε�ֵû������һ��������ֵ��������һ���������Ĺ���Ѳ�ѯ��дΪ NOTHING�� �������������ݾͻᱻ��������ı��Ե��������Ҳ����һ�������⡣ �����Ҫ�����Ч��ֵ�������������Чֵ����ʱҪ����һ��������Ϣ�� �������������Ҫ�ô�����������

��һ���棬һ������INSERTһ����ͼ�Ĵ������������������һ���� �����ݷŵ�����ĵط�ȥ��ȡ������ͼ�IJ��롣 ����������UPDATE��DELETEʱ��ͬ�������飬 ��Ϊ����ͼ��ϵ��û�пɹ�ɨ�����ʵ���ݣ��������������Զ�������á� ��ʱֻ�й�����á�

�������߶����õ�������ĸ�����ȡ���ڶ����ݿ��ʹ�á� ������Ϊ�κ��漰������ִ��һ�Ρ������޸IJ�ѯ�������ɶ���IJ�ѯ�� ���������һ��������漰�����У� һ������һ�������ѯ�Ĺ���ͨ�����ܻ��һ����ÿһ�ж��ֱ�ִ��һ�Σ����Ҫִ�кܶ�Σ��Ĵ�����Ҫ��һЩ�� �������������ķ����Ӹ�����ҪԶ�ȹ���ķ����򵥣����Һ����������ֿ�������ȷ���顣

���磺������������

CREATE TABLE computer (
    hostname	text,    -- indexed
    manufacturer    text     -- indexed
);

CREATE TABLE software (
    software	text,    -- indexed
    hostname	text     -- indexed
);

�������кü�ǧ�У�����hostname�ϵ�������Ψһ�ġ� ����/������Ӧ��ʵ������һ��Լ�������Լ����software����ɾ��������ɾ����������С������������������������

DELETE FROM software WHERE hostname = $1;

��Ϊ��������Ϊ�� computer ����ɾ����ÿһ���������е���һ�Σ���ô������׼�����ұ����������Ĺ滮�� ��hostname������������Ϊ�������ݡ�����Ӧ������д

CREATE RULE computer_del AS ON DELETE TO computer
    DO DELETE FROM software WHERE hostname = OLD.hostname;

�������ǿ��������ֲ�ͬ��ɾ�������������

DELETE FROM computer WHERE hostname = 'mypc.local.net';

�Ա� computer ʹ�����������٣�����ɨ�貢���ɴ����������IJ�ѯҲ����������ɨ�裨ͬ�����٣��������������IJ�ѯ��һ��

DELETE FROM software WHERE computer.hostname = 'mypc.local.net'
		       AND software.hostname = computer.hostname;

��Ϊ�Ѿ������˺��ʵ��������滮��������һ������Ĺ滮

Nestloop
  ->  Index Scan using comp_hostidx on computer
  ->  Index Scan using soft_hostidx on software

�����ڹ���ʹ�������ʵ��֮��û��̫����ٶȲ��

�����ɾ������ϣ��ɾ������ 2000 �� hostname �� old ��ͷ�ļ������ ���������ܵ����������;�IJ�ѯ��һ����

DELETE FROM computer WHERE hostname >= 'old'
		       AND hostname <  'ole'

�������ӵ�������

DELETE FROM software WHERE computer.hostname >= 'old' AND computer.hostname < 'ole'
                       AND software.hostname = computer.hostname;

��ѯ�Ĺ滮������

Hash Join
  ->  Seq Scan on software
  ->  Hash
      ->  Index Scan using comp_hostidx on computer

��һ�����ܵIJ�ѯ��

DELETE FROM computer WHERE hostname ~ '^old';

���ɹ�������ִ�й滮�ǣ�

Nestloop
  ->  Index Scan using comp_hostidx on computer
  ->  Index Scan using soft_hostidx on software

��������滮��������ʶ���� computer ���hostname ����������������������ڶ���������ʽ�� AND �ķ�ʽ�����һ��ʱͬ���������� software����������������ʽ�IJ�ѯ��һ���� �����������κ� 2000 ��Ҫ��ɾ���ľɼ�����ﱻ����һ�Σ� ����Ƕ� computer ��һ������ɨ��Ͷ� software ��2000������ɨ�衣 �����ʵ�ֽ��������������IJ�ѯʵ��֮�� ���������� software ���ʵ�ʴ�С�������������˳��ɨ����Ƿ��ǿ�һЩ�� 2000 ���� SPI �������ϵIJ�ѯ��ִ����Ҫ��ʱ��ģ� ��ʹ����Ҫʹ�õ������鶼�ܿ��ڻ�������֡�

���ǿ������һ����ѯ��

DELETE FROM computer WHERE manufacurer = 'bim';

ͬ������Ҳ�ᵼ�´� computer ����Ķ���ɾ���� ���Դ�����ͬ������ִ�����ύ�ܶ��ѯ���������ɵ��������

DELETE FROM software WHERE computer.manufacurer = 'bim'
                       AND software.hostname = computer.hostname;

������滮�ֽ��Ƕ���������ɨ���Ƕ��ѭ���� ֻ������ computer ������һ��������

Nestloop
  ->  Index Scan using comp_manufidx on computer
  ->  Index Scan using soft_hostidx on software

���κ�һ������£��ӹ���ϵͳ�����Ķ����ѯ�����������ѯ���漰�����е�������Զ�����

������˵������ֻ�������ǵĶ�����action���������ִ����õ���������ʱ�űȴ������нϴ��ٶȲ��죬 ��ʱ�滮����ʧЧ��