一段SQL代码的压缩:从974行到96行,十倍压缩
Posted songxinya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一段SQL代码的压缩:从974行到96行,十倍压缩相关的知识,希望对你有一定的参考价值。
涉及到数据库拆分,需要将相关表的JOIN SQL剥离。
剥离过程中,发现了这么个SQL XML。整个SQL 974 行,分为6个函数。读起来费劲无比。
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 3 <mapper namespace="VisitStatisticsDao"> 4 <resultMap id="visitStatisticsResult" type="com.qunar.hotel.crm.dto.VisitStatisticsResult"/> 5 6 7 <sql id="timeRange"> 8 <![CDATA[ 9 visit.visit_date >= #{dateFrom} ]]> 10 and 11 <![CDATA[ visit.visit_date <= #{dateTo} ]]> 12 </sql> 13 14 <sql id="validState"> 15 and 16 visit.valid = 1 17 </sql> 18 19 <sql id="conditionOfSort"> 20 <choose> 21 <when test="orderBy == \'byDoorVisited\'"> 22 and visit.visit_type = 2 23 </when> 24 <when test="orderBy == \'KPVisited\'"> 25 and visit.is_active = 0 26 </when> 27 <when test="orderBy == \'byDoorKPVisited\'"> 28 and visit.is_active = 0 and visit.visit_type =2 29 </when> 30 <when test="orderBy == \'revisited\'"> 31 and visit.revisit_valid = 1 32 </when> 33 </choose> 34 </sql> 35 36 <sql id="statisticAttribute"> 37 total_count.visit_count as totalVisited, 38 by_door_count.visit_count as byDoorVisited, 39 kp_count.visit_count as KPVisited, 40 by_door_kp_count.visit_count as byDoorKPVisited, 41 by_valid_revisit_count.visit_count as revisited 42 </sql> 43 44 <sql id="order"> 45 <choose> 46 <when test="orderBy == \'totalVisited\'"> 47 totalVisited 48 </when> 49 <when test="orderBy == \'byDoorVisited\'"> 50 byDoorVisited 51 </when> 52 <when test="orderBy == \'KPVisited\'"> 53 KPVisited 54 </when> 55 <when test="orderBy == \'byDoorKPVisited\'"> 56 byDoorKPVisited 57 </when> 58 <when test="orderBy == \'revisited\'"> 59 revisited 60 </when> 61 <otherwise> 62 totalVisited 63 </otherwise> 64 </choose> 65 <choose> 66 <when test="orderType == \'asc\'"> 67 asc 68 </when> 69 <otherwise> 70 desc 71 </otherwise> 72 </choose> 73 </sql> 74 75 <sql id="parent"> 76 (select 77 `id`, 78 `name`, 79 `tree_code`, 80 `org_type` 81 from qadmin_org org 82 where org_type = #{dimension} 83 and org.tree_code like concat((select tree_code from qadmin_org where id = #{groupId}), \'%\') 84 ) 85 parent 86 </sql> 87 88 <sql id="orgVisitRecord"> 89 qadmin_org org 90 left join 91 qadmin_user user 92 on org.id = user.org_id 93 inner join 94 crm_visit_record visit 95 on user.user_id = visit.user_id 96 where 97 <include refid="timeRange"/> 98 <include refid="validState"/> 99 </sql> 100 101 <sql id="bdTotalCount"> 102 (select 103 count(visit.id) visit_count, 104 user.user_id user_id, 105 user.realname real_name 106 from 107 qadmin_org org 108 inner join 109 qadmin_user user on user.org_id = org.id 110 left join 111 (select id, user_id from 112 crm_visit_record visit 113 where 114 <include refid="timeRange"/> 115 <include refid="validState"/> 116 ) 117 visit 118 on visit.user_id = user.user_id 119 where 120 org.tree_code like concat((select tree_code from qadmin_org where id=#{groupId}), \'%\') 121 group by 122 user.user_id 123 ) 124 </sql> 125 126 <sql id="bdByDoorCount"> 127 (select 128 count(visit.id) visit_count, 129 user.user_id user_id, 130 user.realname real_name 131 from 132 qadmin_org org 133 inner join 134 qadmin_user user on user.org_id = org.id 135 left join 136 (select id, user_id from 137 crm_visit_record visit 138 where 139 <include refid="timeRange"/> 140 and 141 visit_type = 2 142 <include refid="validState"/> 143 ) 144 visit on visit.user_id = user.user_id 145 where 146 org.tree_code like concat((select tree_code from qadmin_org where id=#{groupId}), \'%\') 147 group by 148 user.user_id 149 ) 150 </sql> 151 152 <sql id="bdKPCount"> 153 (select 154 count(visit.id) visit_count, 155 user.user_id user_id, 156 user.realname real_name 157 from 158 qadmin_org org 159 inner join 160 qadmin_user user on user.org_id = org.id 161 left join 162 (select id, user_id from 163 crm_visit_record visit 164 where 165 <include refid="timeRange"/> 166 and 167 is_active = 0 168 <include refid="validState"/> 169 ) 170 visit on visit.user_id = user.user_id 171 where 172 org.tree_code like concat((select tree_code from qadmin_org where id=#{groupId}), \'%\') 173 group by 174 user.user_id 175 ) 176 </sql> 177 178 <sql id="bdKPByDoorCount"> 179 (select 180 count(visit.id) visit_count, 181 user.user_id user_id, 182 user.realname real_name 183 from 184 qadmin_org org 185 inner join 186 qadmin_user user on user.org_id = org.id 187 left join 188 (select id, user_id from 189 crm_visit_record visit 190 where 191 <include refid="timeRange"/> 192 and 193 is_active = 0 194 and 195 visit_type = 2 196 <include refid="validState"/> 197 ) 198 visit on visit.user_id = user.user_id 199 where 200 org.tree_code like concat((select tree_code from qadmin_org where id=#{groupId}), \'%\') 201 group by 202 user.user_id 203 ) 204 </sql> 205 206 <sql id="RevisitValidCount"> 207 ( 208 select 209 count(visit.id) visit_count, 210 user.user_id user_id, 211 user.realname real_name 212 from 213 qadmin_org org 214 inner join 215 qadmin_user user on user.org_id = org.id 216 left join 217 (select id, user_id from crm_visit_record visit 218 where 219 <include refid="timeRange"/> 220 <include refid="validState"/> 221 and 222 revisit_valid = 1 223 ) 224 visit on visit.user_id = user.user_id 225 where 226 org.tree_code like concat((select tree_code from qadmin_org where id=#{groupId}), \'%\') 227 group by 228 user.user_id 229 ) 230 </sql> 231 232 <sql id="queryBDVisitedCountInMultiGroupTotal"> 233 ( 234 select 235 org.id as \'id\', 236 org.user_id as \'user_id\', 237 org.sub_realname as \'name\', 238 visit.visit_count as \'visit_count\' 239 from 240 ( 241 select 242 sub.id as \'id\', 243 sub.user_id as \'user_id\', 244 sub.realname as \'sub_realname\' 245 from 246 (select id, name, tree_code from qadmin_org) 247 parent 248 left join 249 (select 250 org.name, 251 org.tree_code, 252 user.id as \'id\', 253 user.user_id as \'user_id\', 254 user.realname 255 from 256 qadmin_org 257 org 258 inner join 259 qadmin_user user 260 on user.org_id = org.id 261 ) 262 sub 263 on sub.tree_code like concat(parent.tree_code, \'%\') 264 where 265 parent.id in 266 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 267 #{item} 268 </foreach> 269 ) 270 org 271 left join 272 ( 273 select 274 count(visit.id) as \'visit_count\', 275 user.user_id as \'user_id\' 276 from 277 crm_visit_record visit 278 left join 279 qadmin_user user 280 on 281 visit.user_id = user.user_id 282 where 283 <include refid="timeRange"/> 284 <include refid="validState"/> 285 group by 286 user.user_id 287 ) 288 visit 289 on org.user_id = visit.user_id 290 ) 291 </sql> 292 293 <sql id="queryBDVisitedCountInMultiGroupByDoor"> 294 ( 295 select 296 org.id as \'id\', 297 org.user_id as \'user_id\', 298 org.sub_realname as \'name\', 299 visit.visit_count as \'visit_count\' 300 from 301 ( 302 select 303 sub.id as \'id\', 304 sub.user_id as \'user_id\', 305 sub.realname as \'sub_realname\' 306 from 307 (select id, name, tree_code from qadmin_org) 308 parent 309 left join 310 (select 311 org.name, 312 org.tree_code, 313 user.id as \'id\', 314 user.user_id as \'user_id\', 315 user.realname 316 from 317 qadmin_org 318 org 319 inner join 320 qadmin_user user 321 on user.org_id = org.id 322 ) 323 sub 324 on sub.tree_code like concat(parent.tree_code, \'%\') 325 where 326 parent.id in 327 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 328 #{item} 329 </foreach> 330 ) 331 org 332 left join 333 ( 334 select 335 count(visit.id) as \'visit_count\', 336 user.user_id as \'user_id\' 337 from 338 crm_visit_record visit 339 left join 340 qadmin_user user 341 on 342 visit.user_id = user.user_id 343 where 344 <include refid="timeRange"/> 345 and 346 visit.visit_type = 2 347 <include refid="validState"/> 348 group by 349 user.user_id 350 ) 351 visit 352 on org.user_id = visit.user_id 353 ) 354 </sql> 355 356 <sql id="queryBDVisitedCountInMultiGroupByKPVisit"> 357 ( 358 select 359 org.id as \'id\', 360 org.user_id as \'user_id\', 361 org.sub_realname as \'name\', 362 visit.visit_count as \'visit_count\' 363 from 364 ( 365 select 366 sub.id as \'id\', 367 sub.user_id as \'user_id\', 368 sub.realname as \'sub_realname\' 369 from 370 (select id, name, tree_code from qadmin_org) 371 parent 372 left join 373 ( 374 select 375 org.name, 376 org.tree_code, 377 user.id as \'id\', 378 user.user_id as \'user_id\', 379 user.realname 380 from 381 qadmin_org 382 org 383 inner join 384 qadmin_user 385 user 386 on user.org_id = org.id 387 ) 388 sub 389 on sub.tree_code like concat(parent.tree_code, \'%\') 390 where 391 parent.id in 392 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 393 #{item} 394 </foreach> 395 ) 396 org 397 left join 398 ( 399 select 400 count(visit.id) as \'visit_count\', 401 user.user_id as \'user_id\' 402 from 403 crm_visit_record visit 404 left join 405 qadmin_user user 406 on 407 visit.user_id = user.user_id 408 where 409 <include refid="timeRange"/> 410 and 以上是关于一段SQL代码的压缩:从974行到96行,十倍压缩的主要内容,如果未能解决你的问题,请参考以下文章如何压缩代码'pymssql查询数据从sql到dataframe'?