¾�ΥС�������ʸ�� �� 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9.6 | 9.5 | 9.4 | 9.3 | 9.2 | 9.1 | 9.0 | 8.4 | 8.3 | 8.2 | 8.1 | 8.0 | 7.4 | 7.3 | 7.2

54.6. ����ǥå��������ȿ���ؿ�

amcostestimate�ؿ��ˤϡ�����ǥå����ȶ��˻��ѤǤ��뤳�Ȥ���ޤäƤ���WHERE�礪���ORDER BY��Υꥹ�Ȥ�ޤࡢ����ǥå����������β�ǽ���򵭽Ҥ������Ϳ�����ޤ��� ���δؿ��ϥ���ǥå����˥����������륳���Ȥγ�����WHERE��������١ʤĤޤꥤ��ǥå����������ˤ���Ф����Ԥοƥơ��֥�ˤ�������ˤ��֤��ʤ��ƤϤʤ�ޤ��� ñ��ʾ����ȡ��ۤȤ�ɤ��٤ƤΥ����ȳ����κ�Ȥϡ����ץƥ��ޥ�����ɸ��롼�����ƤӽФ����ȤǹԤ��ޤ��� amcostestimate�ؿ�����Ĥ��Ȥΰ�̣�ϡ�ɸ��γ�����������뤳�Ȥ��Ǥ�����ˡ�����ǥå������������᥽�åɤ�����ǥå�������ͭ���μ��ηϤ��󶡤��뤳�Ȥ��Ǥ���Ȥ������ȤǤ���

���줾���amcostestimate�ؿ��ϰʲ��Υ����ͥ��������ʤ���Ф����ޤ���

void
amcostestimate (PlannerInfo *root,
                IndexPath *path,
                double loop_count,
                Cost *indexStartupCost,
                Cost *indexTotalCost,
                Selectivity *indexSelectivity,
                double *indexCorrelation);

�ǽ��3�ĤΥѥ�᡼�������ϤǤ���

root

��������Ƥ����䤤��碌�˴ؤ���ץ��ʤξ���

path

����ǥå������������ѥ�����θ����ޤ��� �����Ȥ��������ͤ�������٤ƤΥե�����ɤ�ͭ���Ǥ���

loop_count

�����ȳ����λ����оݤȤʤ륤��ǥå���������󤬷����֤��줿����Ǥ��� ������̾�ͥ��ȥ롼�ץ��祤������������Ѥ���륹�����β�������礭���ͤˤʤ�ޤ��� �����ȳ�����1��Υ������Τ�����ͤǤ��뤳�Ȥ����դ��Ƥ���������loop_count������礭����硢ʣ���Υ������ˤ����������̤�ߤ�ˤϽ�ʬ���ͤȤ�����Ǥ��礦��

�Ǹ��4�ĤΥѥ�᡼���ϻ����Ϥ��ν��ϤǤ���

*indexStartupCost

����ǥå����ε�ư�����ˤ����륳���Ȥ����ꤵ��ޤ���

*indexTotalCost

����ǥå������������ΤΥ����Ȥ����ꤵ��ޤ���

*indexSelectivity

����ǥå����������٤����ꤵ��ޤ���

*indexCorrelation

����ǥå����������ν��֤��ظ�Υơ��֥�ν��ִ֤���ط��������ꤵ��ޤ���

�����ȳ����ؿ��ϡ�SQL�䤽��¾�μ�³������ǤϤʤ���C����ǽ񤫤�ʤ���Ф����ʤ����Ȥ����դ��Ƥ��������� ��ͳ�ϥץ���/���ץƥ��ޥ����������ǡ�����¤�˥����������ʤ���Ф����ʤ�����Ǥ���

����ǥå����������������Ȥ�src/backend/optimizer/path/costsize.c�ǻȤ��롢�༡Ū�ʥǥ������֥��å��μ��Ф��ˤ�seq_page_cost�Υ����Ȥ�������Ʊ�μ��Ф��ˤ�random_page_cost�Υ����Ȥ��������ơ�1�ĤΥ���ǥå����Ԥν����ˤ��̾�cpu_index_tuple_cost�Ȥ��������Ȥ������롢�Ȥ����ѥ�᡼���Ƿ׻�����ʤ���Фʤ�ޤ��� ����ˡ�����ǥå����������ä�indexquals���Τ�ɾ���ˤδ֤˸ƤӽФ������ӱ黻���٤Ƥ��Ф��ơ�cpu_operator_cost��Ŭ���ʷ����򤫤��������Ȥ�������ޤ���

�������������Ȥϡ�����ǥå������ȤΥ������ȴط����뤹�٤ƤΥǥ�������CPU�����Ȥ�ޤ�٤��Ǥ���������ǥå����Ǽ��̤����ƥơ��֥�ιԤν�������Фˤ����륳���Ȥϴޤ�Ƥ������ޤ�����

"��ư�ѥ�����"�ϡ��ǽ�ιԤ���Ф��Ϥ�뤳�Ȥ��Ǥ���褦�ˤʤ�������䤵��ʤ���Фʤ�ʤ���������󥳥��Ȥΰ����Ǥ��� �ۤȤ�ɤΥ���ǥå����Ǥϡ�����ϥ����Ȥ��뤳�Ȥ��Ǥ��ޤ��� ���������⤤��ư�ѥ����Ȥ���ĥ���ǥå�������ǤϤ�����󥼥��ˤ��뤳�Ȥ򴫤�ޤ���

indexSelectivity�ϡ�����ǥå����������δ֤���Ф����ƥơ��֥�ιԤγ������줿���Ȥ������ꤵ���٤��Ǥ��� ��ĵ��䤤��碌�ξ��Ϥ����ͤ���Ϳ����줿�������ºݤ��̲᤹��Ԥγ�����⤯�ʤ뤳�Ȥ��褯����ޤ���

indexCorrelation�ϡ�����ǥå����ν��֤ȥơ��֥�ν��֤δ֤Ρ�-1.0����1.0�ޤǤδ֤��ͤ������ؤȤ������ꤵ���٤��Ǥ��� �����ͤϡ��ᥤ��ơ��֥뤫��Ԥ���Ф�����Υ����ȳ�����Ĵ�����뤿��˻��Ѥ���ޤ���

loop_count���ͤ�1����礭����硢����ͤϥ���ǥå��������Ѥ���1��Υ����������ꤷ��ʿ���ͤǤ���٤��Ǥ���

�����ȳ���

ŵ��Ū�ʥ����ȳ����ϼ��Τ褦�˿ʤ���ޤ���

  1. Ϳ����줿������˴�Ť���ˬ�����ᥤ��ơ��֥�ιԤγ��򳵻������֤��ޤ��� ����ǥå�������ͭ���μ��ηϤ�����ʤ���硢ɸ��Υ��ץƥ��ޥ����δؿ��Ǥ���clauselist_selectivity()����Ѥ��Ƥ���������

    *indexSelectivity = clauselist_selectivity(root, path->indexquals,
                                               path->indexinfo->rel->relid,
                                               JOIN_INNER, NULL);

  2. ����������ˬ����륤��ǥå����ιԿ��򳵻����ޤ��� ¿���Υ���ǥå�������Ǥϡ������indexSelectivity�ȥ���ǥå�������ˤ���Կ���ݤ�����Τ��������Ǥ�����������¿�����⤢��ޤ��� �ʥڡ�������ӹ���Υ���ǥå����Υ�������path->indexinfo��¤�Τ������뤳�Ȥ��Ǥ��뤳�Ȥ����դ��Ƥ�����������

  3. ������������Ф���륤��ǥå����ڡ������򳵻����ޤ��� �����ñ��indexSelectivity�˥ڡ�����Υ���ǥå����Υ�������ݤ�����Τˤʤ�Ǥ��礦��

  4. ����ǥå����������������Ȥ�׻����ޤ��� ����Ū�ʳ����ˤ����Ƥϰʲ��Τ褦�˹Ԥ��Ǥ��礦��

    /*
     * Our generic assumption is that the index pages will be read
     * sequentially, so they cost seq_page_cost each, not random_page_cost.
     * Also, we charge for evaluation of the indexquals at each index row.
     * All the costs are assumed to be paid incrementally during the scan.
     */
    cost_qual_eval(&index_qual_cost, path->indexquals, root);
    *indexStartupCost = index_qual_cost.startup;
    *indexTotalCost = seq_page_cost * numIndexPages +
        (cpu_index_tuple_cost + index_qual_cost.per_tuple) * numIndexTuples;

    ����������ǤϷ����֤���륤��ǥå����������ˤ����륤��ǥå����ɤ߹��ߤˤĤ��Ƹ������Ѥ��θ���Ƥ��ޤ���

  5. ����ǥå�������ؤ򳵻����ޤ��� 1�ĤΥե�����ɤ��Ф���ñ��ʽ��֤Υ���ǥå����Ǥϡ������pg_statistic�������ꤹ�뤳�Ȥ��Ǥ��ޤ��� ��ؤ�̤�Τξ�硢�������ѿ������ͤ���ȥ�����̵��ءˤȤʤ�ޤ���

�����ȳ����ؿ������src/backend/utils/adt/selfuncs.c�ˤ���ޤ���