瀏覽代碼

modules/interconnectroute: optimised sql calls for orig and term

Richard Good 10 年之前
父節點
當前提交
8f47bce676
共有 1 個文件被更改,包括 103 次插入23 次删除
  1. 103 23
      modules/interconnectroute/db.c

+ 103 - 23
modules/interconnectroute/db.c

@@ -5,31 +5,111 @@ db1_con_t * interconnectroute_dbh = NULL;
 db_func_t interconnectroute_dbf;
 
 static char *orig_route_data_query = "select TFROM.INTERNAL_ID as FROM_TRUNK_ID, TTO.INTERNAL_ID as TO_TRUNK_ID, RT.ROUTE_ID as ROUTE_ID from "
-    "service_rate SR "
-    "join interconnect_trunk TFROM on TFROM.INTERCONNECT_PARTNER_ID = SR.FROM_INTERCONNECT_PARTNER_ID "
-    "join interconnect_trunk TTO on TTO.INTERCONNECT_PARTNER_ID = SR.TO_INTERCONNECT_PARTNER_ID " 
-    "join interconnect_route RT on TTO.EXTERNAL_ID = RT.EXTERNAL_ID "
-    "where "
-    "%.*s >= SR.FROM_START "
-    "and %.*s <= SR.FROM_END "
-    "and %.*s >= SR.TO_START "
-    "and %.*s <= SR.TO_END "
-    "and SR.SERVICE_CODE = '%.*s' "
-    "and SR.LEG = '%.*s' "
-    "order by SR.PRIORITY DESC, RT.PRIORITY DESC LIMIT 1";
+"service_rate SR "
+"join interconnect_trunk TFROM on TFROM.INTERCONNECT_PARTNER_ID = SR.FROM_INTERCONNECT_PARTNER_ID "
+"join interconnect_trunk TTO on TTO.INTERCONNECT_PARTNER_ID = SR.TO_INTERCONNECT_PARTNER_ID "
+"join interconnect_route RT on TTO.EXTERNAL_ID = RT.EXTERNAL_ID "
+"where "
+"FROM_INTERCONNECT_PARTNER_ID = "
+"( "
+"SELECT IPID "
+"FROM "
+"( "
+"( "
+"SELECT INTERCONNECT_PARTNER_ID AS IPID, 1000000 AS PRIORITY "
+"FROM ported_number "
+"WHERE NUMBER = %.*s "
+") "
+"UNION "
+"( "
+"SELECT FROM_INTERCONNECT_PARTNER_ID AS IPID, PRIORITY "
+"FROM service_rate "
+"WHERE '%.*s' like concat(FROM_PREFIX,'%') "
+"ORDER BY length(FROM_PREFIX) desc limit 1 "
+") "
+") "
+"AS tmp "
+"ORDER BY tmp.PRIORITY DESC "
+"LIMIT 1 "
+") "
+"and TO_INTERCONNECT_PARTNER_ID = "
+"( "
+"SELECT IPID "
+"FROM "
+"( "
+"( "
+"SELECT INTERCONNECT_PARTNER_ID AS IPID, 1000000 AS PRIORITY "
+"FROM ported_number "
+"WHERE NUMBER = %.*s "
+") "
+"UNION "
+"( "
+"SELECT TO_INTERCONNECT_PARTNER_ID AS IPID, PRIORITY "
+"FROM service_rate "
+"WHERE '%.*s' like concat(TO_PREFIX,'%') "
+"ORDER BY length(TO_PREFIX) desc limit 1 "
+") "
+") "
+"AS tmp "
+"ORDER BY tmp.PRIORITY DESC "
+"LIMIT 1 "
+") "
+"and SR.SERVICE_CODE = '%.*s' "
+"and SR.LEG = '%.*s' "
+"order by SR.PRIORITY DESC, RT.PRIORITY DESC LIMIT 1;";
 
 static char *term_route_data_query = "select TFROM.INTERNAL_ID as FROM_TRUNK_ID, TTO.INTERNAL_ID as TO_TRUNK_ID from "
-    "service_rate SR "
-    "join interconnect_trunk TFROM on TFROM.INTERCONNECT_PARTNER_ID = SR.FROM_INTERCONNECT_PARTNER_ID "
-    "join interconnect_trunk TTO on TTO.INTERCONNECT_PARTNER_ID = SR.TO_INTERCONNECT_PARTNER_ID "
-    "where "
-    "%.*s >= SR.TO_START "
-    "and %.*s <= SR.TO_END "
-    "and %.*s >= SR.FROM_START "
-    "and %.*s <= SR.FROM_END "
-    "and SR.SERVICE_CODE = '%.*s' "
-    "and SR.LEG = '%.*s' "
-    "and TFROM.EXTERNAL_ID = '%.*s';";
+"service_rate SR "
+"join interconnect_trunk TFROM on TFROM.INTERCONNECT_PARTNER_ID = SR.FROM_INTERCONNECT_PARTNER_ID "
+"join interconnect_trunk TTO on TTO.INTERCONNECT_PARTNER_ID = SR.TO_INTERCONNECT_PARTNER_ID "
+"where "
+"FROM_INTERCONNECT_PARTNER_ID = "
+"( "
+"SELECT IPID "
+"FROM "
+"( "
+"( "
+"SELECT INTERCONNECT_PARTNER_ID AS IPID, 1000000 AS PRIORITY "
+"FROM ported_number "
+"WHERE NUMBER = %.*s "
+") "
+"UNION "
+"( "
+"SELECT FROM_INTERCONNECT_PARTNER_ID AS IPID, PRIORITY "
+"FROM service_rate "
+"WHERE '%.*s' like concat(FROM_PREFIX,'%') "
+"ORDER BY length(FROM_PREFIX) desc limit 1 "
+") "
+") "
+"AS tmp "
+"ORDER BY tmp.PRIORITY DESC "
+"LIMIT 1 "
+") "
+"and TO_INTERCONNECT_PARTNER_ID = "
+"( "
+"SELECT IPID "
+"FROM "
+"( "
+"( "
+"SELECT INTERCONNECT_PARTNER_ID AS IPID, 1000000 AS PRIORITY "
+"FROM ported_number "
+"WHERE NUMBER = %.*s "
+") "
+"UNION "
+"( "
+"SELECT TO_INTERCONNECT_PARTNER_ID AS IPID, PRIORITY "
+"FROM service_rate "
+"WHERE '%.*s' like concat(TO_PREFIX,'%') "
+"ORDER BY length(TO_PREFIX) desc limit 1 "
+") "
+") "
+"AS tmp "
+"ORDER BY tmp.PRIORITY DESC "
+"LIMIT 1 "
+") "
+"and SR.SERVICE_CODE = '%.*s' "
+"and SR.LEG = '%.*s' "
+"and TFROM.EXTERNAL_ID = '%.*s' LIMIT 1;";
 
 static char query[QUERY_LEN];