{"id":222718,"date":"2021-07-28T08:13:55","date_gmt":"2021-07-28T00:13:55","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=222718"},"modified":"2021-07-19T09:16:01","modified_gmt":"2021-07-19T01:16:01","slug":"sql-index-method","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sql-index-method.html","title":{"rendered":"SQL\u6027\u80fd\u4f18\u5316\u4e4b\u7d22\u5f15\u4f18\u5316\u6cd5"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nSQL\u4f18\u5316\u662f\u4f18\u5316\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u4f1a\u6d89\u53ca\u7684\u95ee\u9898\uff0c\u7531\u4e8e\u65e9\u671f\u7684\u5f00\u53d1\u4eba\u5458\u5f80\u5f80\u53ea\u5173\u6ce8\u4e8eSQL\u529f\u80fd\u7684\u5b9e\u73b0\uff0c\u800c\u5ffd\u7565\u4e86\u6027\u80fd\u3002\u7279\u522b\u662f\u590d\u6742\u7684SQL\uff0c\u4e0a\u7ebf\u4e4b\u540e\u5f88\u5c11\u4fee\u6539\uff0c\u4e00\u65e6\u51fa\u73b0\u95ee\u9898\uff0c\u5373\u4f7f\u662f\u5f53\u521d\u7684\u5f00\u53d1\u4eba\u5458\u81ea\u5df1\u4e5f\u5f88\u96be\u7406\u6e05\u5176\u4e2d\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u9700\u8981\u82b1\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u53bb\u7406\u89e3\u4ee3\u7801\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6700\u7ec8\u53ef\u80fd\u8fd8\u662f\u611f\u89c9\u65e0\u4ece\u4e0b\u624b\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

SQL\u4f18\u5316\u662f\u4f18\u5316\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u4f1a\u6d89\u53ca\u7684\u95ee\u9898\uff0c\u7531\u4e8e\u65e9\u671f\u7684\u5f00\u53d1\u4eba\u5458\u5f80\u5f80\u53ea\u5173\u6ce8\u4e8eSQL\u529f\u80fd\u7684\u5b9e\u73b0\uff0c\u800c\u5ffd\u7565\u4e86\u6027\u80fd\u3002\u7279\u522b\u662f\u590d\u6742\u7684SQL\uff0c\u4e0a\u7ebf\u4e4b\u540e\u5f88\u5c11\u4fee\u6539\uff0c\u4e00\u65e6\u51fa\u73b0\u95ee\u9898\uff0c\u5373\u4f7f\u662f\u5f53\u521d\u7684\u5f00\u53d1\u4eba\u5458\u81ea\u5df1\u4e5f\u5f88\u96be\u7406\u6e05\u5176\u4e2d\u7684\u4e1a\u52a1\u903b\u8f91\uff0c\u9700\u8981\u82b1\u8d39\u5927\u91cf\u7684\u65f6\u95f4\u53bb\u7406\u89e3\u4ee3\u7801\u4e4b\u95f4\u7684\u5173\u7cfb\uff0c\u6700\u7ec8\u53ef\u80fd\u8fd8\u662f\u611f\u89c9\u65e0\u4ece\u4e0b\u624b\u3002\u56e0\u6b64\u5f00\u53d1\u4eba\u5458\u524d\u671f\u5e94\u505a\u597d\u4ee3\u7801\u6ce8\u91ca\uff0c\u907f\u514d\u7f16\u5199\u8fc7\u4e8e\u590d\u6742\u7684SQL\u8bed\u53e5\u3002\u672c\u6587\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e00\u4e9b\u751f\u4ea7\u73af\u5883\u4e2d\u771f\u5b9e\u7684\u5e38\u7528\u7d22\u5f15\u4f18\u5316\u65b9\u6cd5\u3002<\/p>\n

\"\"<\/p>\n

\u9047\u5230\u95ee\u9898SQL\u65f6\uff0c\u5927\u5bb6\u53ef\u4ee5\u6839\u636e\u5404\u81ea\u7684\u4e60\u60ef\u4f7f\u7528\u4e0d\u540c\u7684\u5de5\u5177(PL\/SQL\u3001TOAD\u7b49)\u5bf9SQL\u8fdb\u884c\u683c\u5f0f\u5316\uff0c\u6211\u4eec\u9700\u8981\u91cd\u70b9\u5173\u6ce8\u7684\u662fFROM\u540e\u9762\u7684\u8868\uff0c\u4ee5\u53ca\u5305\u542bWHERE\u8bed\u53e5\u7684\u6761\u4ef6\uff0c\u7136\u540e\u901a\u8fc7awrsqrpt\u6216dbms_xplan\u83b7\u53d6SQL\u7684\u8be6\u7ec6\u6267\u884c\u8ba1\u5212\u548c\u8d44\u6e90\u6d88\u8017\u4fe1\u606f\uff0c\u4e1a\u52a1\u6848\u4f8b\u4e2d\u7684SQL\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n

SQL> select sum(cggzl) cggzl, sum(qbgzl) qbgzl \r\n  from (select case \r\n                 when zlxm_mc like '%2\u00ea3?3\uffe11??\u00ec2\u00e9%' then \r\n                  gzl \r\n                 else \r\n                  0 \r\n               end cggzl, \r\n               case \r\n                 when zlxm_mc like '%?3\u00b1\u00ed?\u00f71\u00f9%' then \r\n                  gzl \r\n                 else \r\n                  0 \r\n               end qbgzl \r\n          from dictmanage.dict_zl_pro   b, \r\n               his.pat_inpat_order_info c, \r\n               pat_inpat_order_cost     d \r\n         where d.sfxm_id = b.zlxm_id \r\n           and c.yzjl_id = d.dyzy_yzjl_id \r\n           and zlxm_mc like '%2???%' \r\n           and c.yz_zxrq >= to_date(sysdate) \r\n           and c.yz_zxrq < to_date(sysdate + 1) \r\n           and d.fy_status in ('1', '2') \r\n           and sfxm_je > 0 \r\n           and c.yz_zfrq is null \r\n           and c.zylsh = :in_zylsh) <\/pre>\n

SQL\u7684\u8be6\u7ec6\u6267\u884c\u8ba1\u5212\u5982\u56fe1\u6240\u793a\u3002<\/p>\n

\"\"<\/p>\n

AWR\u62a5\u544a\u4e2d\u7684\u8d44\u6e90\u6d88\u8017\u4fe1\u606f\u5982\u56fe2\u6240\u793a\u3002<\/p>\n

\"\"<\/p>\n

\u4e0a\u8ff0\u4ee3\u7801\u6240\u793a\u7684\u4e1a\u52a1SQL\u8bed\u53e5\u901a\u8fc7\u4e09\u5f20\u8868\u8fdb\u884c\u5173\u8054\uff0c\u6700\u7ec8\u8fd4\u56de\u7684\u884c\u6570\u4e3a\u4e2a\u4f4d\u6570\uff0c\u4ece\u6267\u884c\u8ba1\u5212\u4e2d\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0cId=0\uff0cCBO\u8ba1\u7b97\u603b\u7684COST\u4e3a123K\uff0c\u5176\u4e2d\u7edd\u5927\u90e8\u5206\u7684COST\u662f\u7531Id=10\u7684\u8868pat_inpat_order_cost\u5168\u8868\u626b\u63cf\u6240\u4ea7\u751f\u7684\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u9700\u8981\u91cd\u70b9\u5173\u6ce8 pat_inpat_order_cost\u4e0e\u5176\u4ed6\u4e24\u5f20\u8868\u683c\u7684\u5173\u8054\u60c5\u51b5\uff0cwhere\u6761\u4ef6\u4e2d\uff0cpat_inpat_order_cost\u7684sfxm_id\u548cdyzy_yzjl_id\u9664\u4e86\u4e0e\u5176\u4ed6\u4e24\u5f20\u8868\u7684\u5b57\u6bb5\u76f8\u5173\u8054\u4e4b\u5916\uff0c\u53ea\u6709fy_status\u4e00\u4e2a\u8fc7\u6ee4\u6761\u4ef6\uff0c\u4e0b\u9762\u6211\u4eec\u5c31\u6765\u770b\u4e0b\u8be5\u5217\u7684\u9009\u62e9\u6027\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

SQL> select \/*+ NO_MERGE LEADING(a b) *\/ \r\n b.owner, \r\n b.table_name, \r\n a.column_name, \r\n b.num_rows, \r\n a.num_distinct Cardinality, \r\n ROUND(A.num_distinct * 100 \/ B.num_rows, 1) selectivity \r\n  from dba_tab_col_statistics a, dba_tables b \r\n where a.owner = b.owner \r\n   and a.table_name = b.table_name \r\n   and a.owner = upper('his') \r\n   and a.table_name = upper('pat_inpat_order_cost') \r\n   and a.column_name = upper('fy_status'); <\/pre>\n

pat_inpat_order_cost\u8868\u7684\u5b57\u6bb5\u4fe1\u606f\u5982\u56fe3\u6240\u793a\u3002<\/p>\n

\"\"<\/p>\n

SQL> select count(*), FY_STATUS \r\n  from his.pat_inpat_order_cost c \r\n group by FY_STATUS; <\/pre>\n

fy_status\u5b57\u6bb5\u5217\u7684\u9009\u62e9\u6027\u5982\u56fe4\u6240\u793a\u3002<\/p>\n

\"\"<\/p>\n

\u7531\u56fe4\u53ef\u77e5\uff0cfy_status\u7684\u9009\u62e9\u6027\u5e76\u4e0d\u597d\uff0c\u800c\u4e14\u5b58\u5728\u4e25\u91cd\u503e\u659c\uff0c\u8bed\u53e5\u4e2d\u7684\u56fa\u5b9a\u5199\u6cd5d.fy_status in ('1', '2')\u51e0\u4e4e\u5305\u542b\u4e86\u6240\u6709\u8bb0\u5f55\uff0c\u56e0\u6b64\u5176\u5e76\u4e0d\u662f\u4e00\u4e2a\u5f88\u597d\u7684\u8fc7\u6ee4\u6761\u4ef6\u3002where\u6761\u4ef6\u4e2d\u7684\u5927\u90e8\u5206\u8fc7\u6ee4\u6761\u4ef6\u5747\u6765\u81ea\u4e8eC\u8868pat_inpat_order_info\uff0c\u800c\u4e14C\u8868\u4e0eD\u8868pat_inpat_order_cost\u7684sfxm_id\u5b57\u6bb5\u76f8\u5173\u8054\u3002<\/p>\n

\u6574\u4e2aSQL\u8bed\u53e5\u6700\u7ec8\u8fd4\u56de\u7684\u884c\u6570\u4e3a\u4e2a\u4f4d\u6570\uff0cC\u8868\u901a\u8fc7YZ_ZXRQ_IDX\u7d22\u5f15\u8303\u56f4\u626b\u63cf\u518d\u56de\u8868\u8fdb\u884c\u8fc7\u6ee4\uff0c\u83b7\u53d6\u7ed1\u5b9a\u53d8\u91cf\u503c\uff0c\u4e4b\u540e\u518d\u8fdb\u4e00\u6b65\u786e\u8ba4C\u8868\u8fd4\u56de\u7684\u884c\u6570\uff0c\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

SQL> select sql_Id, name, datatype_string, last_captured, value_string \r\n  from v$sql_bind_capture \r\n where sql_id = '18rwad2bgcxfa'; <\/pre>\n

SQL\u7ed1\u5b9a\u53d8\u91cf\u503c\u83b7\u53d6\u60c5\u51b5\u5982\u56fe5\u6240\u793a\u3002<\/p>\n

\"\"<\/p>\n

SQL> select count(*) \r\n  from his.pat_inpat_order_info c \r\n where c.yz_zxrq >= to_date(sysdate) \r\n   and c.yz_zxrq < to_date(sysdate + 1) \r\n   and c.yz_zfrq is null \r\n   and c.zylsh = 72706; <\/pre>\n

\u5e26\u5165\u7ed1\u5b9a\u53d8\u91cf\u6211\u4eec\u53ef\u4ee5\u53d1\u73b0\uff0c\u8fd9\u4e2a\u67e5\u8be2\u8fd4\u56de\u7684\u884c\u6570\u90fd\u4fdd\u6301\u5728\u4e2a\u4f4d\u6570\uff0c\u5982\u679cC\u8868\u548cD\u8868\u91c7\u7528\u5d4c\u5957\u8fde\u63a5\u7684\u65b9\u5f0f\uff0cC\u8868\u80fd\u4f5c\u4e3a\u9a71\u52a8\u8868\u4e0eD\u8868pat_inpat_order_cost\u76f8\u5173\u8054\uff0c\u88ab\u9a71\u52a8\u8868\u53ea\u9700\u8981\u5728\u5173\u8054\u5217\u4e0a\u521b\u5efa\u7d22\u5f15\uff0c\u5373\u53ef\u5927\u5e45\u63d0\u5347\u6574\u4e2a\u67e5\u8be2\u7684\u6548\u7387\uff0c\u505a\u6cd5\u5176\u5b9e\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u5728sfxm_id\u5b57\u6bb5\u4e0a\u521b\u5efa\u7d22\u5f15\u5373\u53ef\uff0c\u547d\u4ee4\u5982\u4e0b\uff1a<\/p>\n

SQL> create index IDX_SFXM_ID on PAT_INPAT_ORDER_COST (SFXM_ID); \r\nPlan hash value: 408580053 \r\n------------------------------------------------------------------------------------------------ \r\n| Id  | Operation               | Name                 | Rows  | Bytes | Cost (%CPU)| Time     | \r\n------------------------------------------------------------------------------------------------ \r\n|   0 | SELECT STATEMENT        |                      |       |       |    12 (100)|          | \r\n|   1 |  SORT AGGREGATE         |                      |     1 |    68 |            |          | \r\n|*  2 |   FILTER                |                      |       |       |            |          | \r\n|   3 |    NESTED LOOPS         |                      |     1 |    68 |    12   (0)| 00:00:01 | \r\n|   4 |     NESTED LOOPS        |                      |     1 |    68 |    12   (0)| 00:00:01 | \r\n|   5 |      NESTED LOOPS       |                      |     1 |    39 |    11   (0)| 00:00:01 | \r\n|*  6 |       TABLE ACCESS BY GLOBAL INDEX ROWID \r\n                                | PAT_INPAT_ORDER_INFO |     1 |    21 |     5   (0)| 00:00:01 | \r\n|*  7 |        INDEX RANGE SCAN | YZ_ZXRQ_IDX          |     4 |       |     3   (0)| 00:00:01 | \r\n|*  8 |       TABLE ACCESS BY GLOBAL INDEX ROWID \r\n                                | PAT_INPAT_ORDER_COST |     6 |   108 |     6   (0)| 00:00:01 | \r\n|*  9 |        INDEX RANGE SCAN | IDX_DYZY_YZJL_ID     |     6 |       |     2   (0)| 00:00:01 | \r\n|* 10 |      INDEX UNIQUE SCAN  | DICT_ZL_PRO_PK       |     1 |       |     0   (0)|          | \r\n|* 11 |     TABLE ACCESS BY INDEX ROWID | DICT_ZL_PRO  |     1 |    29 |     1   (0)| 00:00:01 | \r\n------------------------------------------------------------------------------------------------ \r\nPredicate Information (identified by operation id): \r\n--------------------------------------------------- \r\n   2 - filter(TO_DATE(TO_CHAR(SYSDATE@!+1))>TO_DATE(TO_CHAR(SYSDATE@!))) \r\n   6 - filter((\"C\".\"ZYLSH\"=TO_NUMBER(:IN_ZYLSH) AND \"C\".\"YZ_ZFRQ\" IS NULL)) \r\n   7 - access(\"C\".\"YZ_ZXRQ\">=TO_DATE(TO_CHAR(SYSDATE@!)) AND \"C\".\"YZ_ZXRQ\"0 AND INTERNAL_FUNCTION(\"D\".\"FY_STATUS\"))) \r\n   9 - access(\"C\".\"YZJL_ID\"=\"D\".\"DYZY_YZJL_ID\") \r\n  10 - access(\"D\".\"SFXM_ID\"=\"B\".\"ZLXM_ID\") \r\n  11 - filter(\"ZLXM_MC\" LIKE '%\u90e8\u4f4d%') <\/pre>\n

\u521b\u5efa\u7d22\u5f15\u4e4b\u540e\uff0c\u6574\u4e2a\u6267\u884c\u8ba1\u5212\u6309\u7167\u6211\u4eec\u8bbe\u60f3\u7684\u65b9\u5f0f\u8fdb\u884c\uff0cSQL\u6267\u884c\u65f6\u95f4\u4e5f\u4ece\u539f\u6765\u768424\u5206\u949f\u7f29\u77ed\u52301\u79d2\uff0c\u901f\u5ea6\u63d0\u5347\u4e86\u4e0a\u5343\u500d\u3002<\/p>\n

\u4e0a\u8ff0\u6848\u4f8b\u4ecb\u7ecd\u4e86\u4e00\u79cd\u6700\u7b80\u5355\u7684SQL\u4f18\u5316\u65b9\u5f0f\uff0c\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u5f88\u96be\u8ba9\u5f00\u53d1\u5546\u4fee\u6539\u5e94\u7528\uff0c\u56e0\u6b64\u7d22\u5f15\u7684\u4f18\u5316\u5728SQL\u4f18\u5316\u5de5\u4f5c\u4e2d\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

SQL\u4f18\u5316\u662f\u4f18\u5316\u5de5\u4f5c\u4e2d\u7ecf\u5e38\u4f1a\u6d89\u53ca\u7684\u95ee\u9898\uff0c\u7531\u4e8e\u65e9\u671f\u7684\u5f00\u53d1\u4eba\u5458\u5f80\u5f80\u53ea\u5173\u6ce8\u4e8eSQL\u529f\u80fd\u7684\u5b9e\u73b0\uff0c\u800c\u5ffd\u7565\u4e86\u6027\u80fd\u3002\u7279\u522b\u662f\u590d […]<\/p>\n","protected":false},"author":1920,"featured_media":61403,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-222718","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=222718"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222718\/revisions"}],"predecessor-version":[{"id":222781,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/222718\/revisions\/222781"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/61403"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=222718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=222718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=222718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}