Chapter 47. �����������㺯��

����: �� Tom Lane���� � 2000-01-24��

ע��: ��Щ�����������Ϊ������д�µ��������ʷ�ʽ�ĸ����һ�µ�һ���֣�

ÿ����������ģʽ�������ṩһ�����ڹ滮��/�Ż����Ŀ������㺯���� ��������Ĺ��� OID �ڷ���ģʽ�� pg_am ��� ��¼�� amcostestimate �ֶ��������

ע��: �� PostgreSQL 7.0 ��ǰ�� ʹ�õ�������һ��ע����������صĿ������㺯����ģʽ��

amcostestimate �����յ�һ�� WHERE �Ӿ䣬��Щ�Ӿ䱻��Ϊ�����������õģ� �������������뷵�ؼ�������ķ��������Ŀ����� WHERE �Ӿ��ѡ���ԣ� Ҳ����˵��������ɨ���������������Ҫ�����������IJ��֣��� ���ڼ򵥵������ �������п����������Ĺ���������ͨ�������Ż������׼�Ĺ�������ɣ� ��Ҫһ�� amcostestimate ������ԭ����������������ģʽ�ṩһЩ����������ص���Ϣ�� �������п��ܸĽ���׼�ļ��㣨Ԥ�ƣ���

ÿ�� amcostestimate ��������������������֣�

void
amcostestimate (Query *root,
                RelOptInfo *rel,
                IndexOptInfo *index,
                List *indexQuals,
                Cost *indexStartupCost,
                Cost *indexTotalCost,
                Selectivity *indexSelectivity,
                double *indexCorrelation);
   

ǰ���ĸ����������룺

root

������IJ�ѯ��

rel

���������Ĺ�ϵ(��)��

index

��������

indexQuals

���������Ӿ��б������� AND����һ�� NIL �б����û�п��õ�������

����ĸ�������ͨ�����ô��ݵ������

*indexStartupCost

����Ϊ������������Ŀ���

*indexTotalCost

����Ϊ����������ܿ���

*indexSelectivity

����Ϊ����ѡ����

*indexCorrelation

��������ɨ��˳����²���˳��֮��������ϵ��

��ע�⿪�����㺯�������� C д�������� SQL �����κο����õĹ������ԣ� ��Ϊ���DZ�����ʹ滮��/�Ż������ڲ����ݽṹ��

�������ʿ���Ӧ����src/backend/optimizer/path/costsize.c ����ʹ�õĵ�λ���㣺 һ��˳����̴洢��ץȡ����Ϊ 1.0�� һ�η�˳��ץȡ�Ŀ���Ϊ random_page_cost�� ���Ҵ���һ��������¼�Ŀ���ͨ��Ӧ�õ��� cpu_index_tuple_cost ������һ���������û����ڵ��Ż��������������⣬Ӧ����һ�� cpu_operator_cost �ĺ��ʵı�����Ϊ���������ڼ��κμ���ıȽϲ����� �������Ǽ��� indexQuals ��������ѯ���Լ�����

���ʿ���Ӧ�ð���������ɨ�������������صĴ��̺� CPU ������ �����Ǽ�������������ʶ�����������Ŀ�����

"��������"��ȫ�����������������ǿ�ʼץȡ��һ����¼֮ǰ�������ĵĿ����� ���ڴ�����������ⲿ�ֿ��Ե����㣬 ����һ�����űȽϸ������������������Ϳ���ϣ�������ֵ����Ϊ���㣮

indexSelectivity ������ѡ���ԣ�Ӧ������Ϊ������ɨ������������¼�ォ���������IJ��֣� �����һ���������ij��ϣ� ������ֽ����Եر�ʵ�ʴ��ݸ��������ʸ������ļ�¼���ָߣ�

indexCorrelation Ӧ������Ϊ����˳��ͱ�˳��������ϵ��(��Χ�� -1.0 �� 1.0 ֮��)�������ڵ�����������ץȡ�еĿ����ļ��㣮

��������

һ�ε��͵Ŀ������������������������У�

  1. ����ͷ��ػ��ڸ������ʸ������Ľ�Ҫ���ʵ�������������� �����֪���κ�����������ص���Ϣ����ʹ�ñ�׼���Ż������� clauselist_selectivity():

    *indexSelectivity = clauselist_selectivity(root, indexQuals,
                                               lfirsti(rel->relids));
         

  2. ���㣨���ƣ���ɨ������н�Ҫ�����ʵ�������¼���� ��������������ͣ�������ֵ��� indexSelectivity ������������ļ�¼������ ���������Ը��࣮ ����ע��������ҳ����Ĵ�С�ͼ�¼���Դӽṹ IndexOptInfo ���ã���

  3. ���㣨���ƣ���ɨ������н�Ҫ��������������ҳ������ ������ֿ���ֻ�� indexSelectivity ������ҳ��������������Ĵ�С��

  4. �����������ʿ�����һ�������ļ�����������������

        /*
         * ������һ���Եļ���������ҳ�潫��˳����룬
         *  �������ÿ���Ŀ���Ϊ1.0��û�� random_page_cose��
         *  ͬ�������Ǽ���ÿ��������¼�����������Ŀ�����
         *  ���п�������������ɨ��������𲽵����ģ���
         *
         * Our generic assumption is that the index pages will be read
         * sequentially, so they have cost 1.0 each, not random_page_cost.
         * Also, we charge for evaluation of the indexquals at each index tuple.
         * All the costs are assumed to be paid incrementally during the scan.
         */
        cost_qual_eval(&index_qual_cost, indexQuals);
        *indexStartupCost = 0;
        *indexTotalCost = numIndexPages +
            (cpu_index_tuple_cost + index_qual_cost.per_tuple) * numIndexTuples;
         

  5. ������������ԣ�������һ���ֶ��ϵļ��������������������Դ� pg_statistic �м�����������������δ֪�����صĹ�������(�����)��

���������������ӿ����� src/backend/utils/adt/selfuncs.c �ҵ���

ͨ����һ�� amcostestimate ������ pg_proc ��¼��Ӧ����ʾ�˸����������в���������Ϊ internal ����Ϊ���� �����Ͷ����� SQL ֪�������ͣ����������ǵķ��������� void��