标签:
涉及到数据库拆分,需要将相关表的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 411 visit.is_active = 0 412 <include refid="validState"/> 413 group by 414 user.user_id 415 ) 416 visit 417 on org.user_id = visit.user_id 418 ) 419 </sql> 420 421 <sql id="queryBDVisitedCountInMultiGroupByKPDoorVisit"> 422 ( 423 select 424 org.id as ‘id‘, 425 org.user_id as ‘user_id‘, 426 org.sub_realname as ‘name‘, 427 visit.visit_count as ‘visit_count‘ 428 from 429 ( 430 select 431 sub.id as ‘id‘, 432 sub.user_id as ‘user_id‘, 433 sub.realname as ‘sub_realname‘ 434 from 435 (select id, name, tree_code from qadmin_org) 436 parent 437 left join 438 ( 439 select 440 org.name, 441 org.tree_code, 442 user.id as ‘id‘, 443 user.user_id as ‘user_id‘, 444 user.realname 445 from 446 qadmin_org 447 org 448 inner join 449 qadmin_user 450 user 451 on user.org_id = org.id 452 ) 453 sub 454 on sub.tree_code like concat(parent.tree_code, ‘%‘) 455 where 456 parent.id in 457 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 458 #{item} 459 </foreach> 460 ) 461 org 462 left join 463 ( 464 select 465 count(visit.id) as ‘visit_count‘, 466 user.user_id as ‘user_id‘ 467 from 468 crm_visit_record visit 469 left join 470 qadmin_user user 471 on 472 visit.user_id = user.user_id 473 where 474 <include refid="timeRange"/> 475 and 476 visit.is_active = 0 477 and 478 visit.visit_type = 2 479 <include refid="validState"/> 480 group by 481 user.user_id 482 ) 483 visit 484 on org.user_id = visit.user_id 485 ) 486 </sql> 487 488 <sql id="queryBDVisitedCountInMultiGroupByValidRevisit"> 489 ( 490 select 491 org.id as ‘id‘, 492 org.user_id as ‘user_id‘, 493 org.sub_realname as ‘name‘, 494 visit.visit_count as ‘visit_count‘ 495 from 496 ( 497 select 498 sub.id as ‘id‘, 499 sub.user_id as ‘user_id‘, 500 sub.realname as ‘sub_realname‘ 501 from 502 (select id, name, tree_code from qadmin_org) 503 parent 504 left join 505 ( 506 select 507 org.name, 508 org.tree_code, 509 user.id as ‘id‘, 510 user.user_id as ‘user_id‘, 511 user.realname 512 from 513 qadmin_org 514 org 515 inner join 516 qadmin_user 517 user 518 on user.org_id = org.id 519 ) 520 sub 521 on sub.tree_code like concat(parent.tree_code, ‘%‘) 522 where 523 parent.id in 524 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 525 #{item} 526 </foreach> 527 ) 528 org 529 left join 530 ( 531 select 532 count(visit.id) as ‘visit_count‘, 533 user.user_id as ‘user_id‘ 534 from 535 crm_visit_record visit 536 left join 537 qadmin_user user 538 on 539 visit.user_id = user.user_id 540 where 541 <include refid="timeRange"/> 542 and 543 visit.revisit_valid = 1 544 <include refid="validState"/> 545 group by 546 user.user_id 547 ) 548 visit 549 on org.user_id = visit.user_id 550 ) 551 </sql> 552 553 <select id="querySortedBDVisitStatistics" parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition" resultType="string"> 554 select user_id from 555 ( 556 select 557 visit.user_id as ‘user_id‘, 558 visit.visit_count as ‘visit_count‘ 559 from 560 ( 561 select 562 sub.user_id as ‘user_id‘ 563 from 564 (select id, tree_code from qadmin_org) 565 parent 566 left join 567 ( 568 select 569 org.tree_code as ‘tree_code‘, 570 user.user_id as ‘user_id‘ 571 from 572 qadmin_org 573 org 574 inner join 575 qadmin_user user 576 on user.org_id = org.id 577 ) 578 sub 579 on sub.tree_code like concat(parent.tree_code, ‘%‘) 580 where 581 parent.id in 582 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 583 #{item} 584 </foreach> 585 ) 586 user 587 left join 588 ( 589 select 590 count(visit.id) as ‘visit_count‘, 591 user_id 592 from crm_visit_record visit 593 where 594 <include refid="timeRange"/> 595 <include refid="validState"/> 596 <include refid="conditionOfSort"/> 597 group by user_id 598 ) 599 visit 600 on user.user_id = visit.user_id 601 order by visit_count 602 <choose> 603 <when test="orderType == ‘asc‘"> 604 asc 605 </when> 606 <otherwise> 607 desc 608 </otherwise> 609 </choose> 610 )temp 611 612 </select> 613 614 <select id="queryBDVisitedCount" resultMap="visitStatisticsResult" 615 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition" 616 fetchSize="100" resultSetType="FORWARD_ONLY"> 617 select 618 total_count.user_id as groupId, 619 total_count.real_name as name, 620 <include refid="statisticAttribute"/> 621 from 622 623 <include refid="bdTotalCount"/> 624 total_count 625 left join 626 <include refid="bdByDoorCount"/> 627 by_door_count 628 on total_count.user_id = by_door_count.user_id 629 left join 630 <include refid="bdKPCount"/> 631 kp_count 632 on total_count.user_id = kp_count.user_id 633 left join 634 <include refid="bdKPByDoorCount"/> 635 by_door_kp_count 636 on total_count.user_id = by_door_kp_count.user_id 637 left join 638 <include refid="RevisitValidCount"/> 639 by_valid_revisit_count 640 on total_count.user_id = by_valid_revisit_count.user_id 641 order by 642 <include refid="order"/> 643 </select> 644 645 <select id="queryOrgVisitedCount" resultMap="visitStatisticsResult" 646 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition" 647 fetchSize="100" resultSetType="FORWARD_ONLY"> 648 select 649 total_count.parent_id as ‘groupId‘, 650 total_count.parent_name as ‘name‘, 651 <include refid="statisticAttribute"/> 652 from 653 ( 654 select 655 parent.id as ‘parent_id‘, 656 parent.name as ‘parent_name‘, 657 count(sub.visit_id) as ‘visit_count‘ 658 from 659 <include refid="parent"/> 660 left join 661 (select 662 org.tree_code as ‘tree_code‘, 663 visit.id as ‘visit_id‘ 664 from 665 <include refid="orgVisitRecord"/> 666 ) 667 sub 668 on sub.tree_code like concat(parent.tree_code, ‘%‘) 669 group by parent.id) 670 total_count 671 left join 672 ( 673 select 674 parent.id as ‘parent_id‘, 675 parent.name as ‘parent_name‘, 676 count(sub.visit_id) as ‘visit_count‘ 677 from 678 <include refid="parent"/> 679 left join 680 (select 681 org.tree_code as ‘tree_code‘, 682 visit.id as ‘visit_id‘ 683 from 684 <include refid="orgVisitRecord"/> 685 and visit.visit_type = 2 686 ) 687 sub 688 on sub.tree_code like concat(parent.tree_code, ‘%‘) 689 group by parent.id) 690 by_door_count 691 on total_count.parent_id = by_door_count.parent_id 692 left join 693 ( 694 select 695 parent.id as ‘parent_id‘, 696 parent.name as ‘parent_name‘, 697 count(sub.visit_id) as ‘visit_count‘ 698 from 699 <include refid="parent"/> 700 left join 701 (select 702 org.tree_code as ‘tree_code‘, 703 visit.id as ‘visit_id‘ 704 from 705 <include refid="orgVisitRecord"/> 706 and visit.is_active = 0 707 ) 708 sub 709 on sub.tree_code like concat(parent.tree_code, ‘%‘) 710 group by parent.id) 711 kp_count 712 on total_count.parent_id = kp_count.parent_id 713 left join 714 ( 715 select 716 parent.id as ‘parent_id‘, 717 parent.name as ‘parent_name‘, 718 count(sub.visit_id) as ‘visit_count‘ 719 from 720 <include refid="parent"/> 721 left join 722 (select 723 org.tree_code as ‘tree_code‘, 724 visit.id as ‘visit_id‘ 725 from 726 <include refid="orgVisitRecord"/> 727 and visit.is_active = 0 728 and visit.visit_type = 2 729 ) 730 sub 731 on sub.tree_code like concat(parent.tree_code, ‘%‘) 732 group by parent.id) 733 kp_by_door_count 734 on total_count.parent_id = kp_by_door_count.parent_id 735 left join 736 ( 737 select 738 parent.id as ‘parent_id‘, 739 parent.name as ‘parent_name‘, 740 count(sub.visit_id) as ‘visit_count‘ 741 from 742 <include refid="parent"/> 743 left join 744 ( 745 select 746 org.tree_code as ‘tree_code‘, 747 visit.id as ‘visit_id‘ 748 from 749 <include refid="orgVisitRecord"/> 750 and visit.revisit_valid = 1 751 ) 752 sub 753 on sub.tree_code like concat(parent.tree_code, ‘%‘) 754 group by parent.id 755 ) 756 by_valid_revisit_count 757 on total_count.parent_id = by_valid_revisit_count.parent_id 758 order by 759 <include refid="order"/> 760 </select> 761 762 <select id="querySingleBDVisitedCount" resultMap="visitStatisticsResult" 763 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition"> 764 select 765 total_count.user_id as groupId, 766 total_count.name as name, 767 <include refid="statisticAttribute"/> 768 from 769 ( 770 select 771 count(visit.id) as visit_count, 772 user.user_id as user_id, 773 user.realname as name 774 from 775 qadmin_user user 776 left join 777 crm_visit_record visit 778 on user.user_id = visit.user_id 779 where 780 user.user_id = #{groupId} 781 and 782 <include refid="timeRange"/> 783 <include refid="validState"/> 784 ) 785 total_count 786 left join 787 ( 788 select 789 count(visit.id) as visit_count, 790 user.user_id as user_id, 791 user.realname as name 792 from 793 qadmin_user user 794 left join 795 crm_visit_record visit 796 on user.user_id = visit.user_id 797 where 798 user.user_id = #{groupId} 799 and 800 <include refid="timeRange"/> 801 <include refid="validState"/> 802 and 803 visit.visit_type = 2 804 ) 805 by_door_count 806 on total_count.user_id = by_door_count.user_id 807 left join 808 ( 809 select 810 count(visit.id) as visit_count, 811 user.user_id as user_id, 812 user.realname as name 813 from 814 qadmin_user user 815 left join 816 crm_visit_record visit 817 on user.user_id = visit.user_id 818 where 819 user.user_id = #{groupId} 820 and 821 <include refid="timeRange"/> 822 <include refid="validState"/> 823 and 824 visit.is_active = 0 825 ) 826 kp_count 827 on total_count.user_id = kp_count.user_id 828 left join 829 ( 830 select 831 count(visit.id) as visit_count, 832 user.user_id as user_id, 833 user.realname as name 834 from 835 qadmin_user user 836 left join 837 crm_visit_record visit 838 on user.user_id = visit.user_id 839 where 840 user.user_id = #{groupId} 841 and 842 <include refid="timeRange"/> 843 <include refid="validState"/> 844 and 845 visit.is_active = 0 846 and 847 visit.visit_type = 2 848 ) 849 by_door_kp_count 850 on total_count.user_id = by_door_kp_count.user_id 851 left join 852 ( 853 select 854 count(visit.id) as visit_count, 855 user.user_id as user_id, 856 user.realname as name 857 from 858 qadmin_user user 859 left join 860 crm_visit_record visit 861 on user.user_id = visit.user_id 862 where 863 user.user_id = #{groupId} 864 and 865 <include refid="timeRange"/> 866 <include refid="validState"/> 867 and 868 visit.revisit_valid = 1 869 870 ) 871 by_valid_revisit_count 872 on total_count.user_id = by_valid_revisit_count.user_id 873 874 </select> 875 876 <select id="queryBDVisitedCountInMultiGroup" resultMap="visitStatisticsResult" 877 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition" 878 fetchSize="100" resultSetType="FORWARD_ONLY"> 879 select 880 total_count.user_id as ‘groupId‘, 881 total_count.name as ‘name‘, 882 <include refid="statisticAttribute"/> 883 from 884 <include refid="queryBDVisitedCountInMultiGroupTotal"/> 885 total_count 886 left join 887 <include refid="queryBDVisitedCountInMultiGroupByDoor"/> 888 by_door_count 889 on 890 total_count.user_id = by_door_count.user_id 891 left join 892 <include refid="queryBDVisitedCountInMultiGroupByKPVisit"/> 893 kp_count 894 on total_count.user_id = kp_count.user_id 895 left join 896 <include refid="queryBDVisitedCountInMultiGroupByKPDoorVisit"/> 897 by_door_kp_count 898 on total_count.user_id = by_door_kp_count.user_id 899 left join 900 <include refid="queryBDVisitedCountInMultiGroupByValidRevisit"/> 901 by_valid_revisit_count 902 on total_count.user_id = by_valid_revisit_count.user_id 903 group by total_count.user_id 904 order by 905 <include refid="order"/> 906 </select> 907 908 <select id="queryBDVisitedRecordSize" resultType="int" 909 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition"> 910 select count(distinct sub.user_id) from 911 (select id, tree_code from qadmin_org) 912 parent 913 left join 914 (select 915 org.tree_code as ‘tree_code‘, 916 `user`.id as ‘user_id‘ 917 from 918 qadmin_org org left join 919 qadmin_user `user` 920 on `user`.org_id = org.id) 921 sub 922 on sub.tree_code like concat(parent.tree_code, ‘%‘) 923 where 924 parent.id = #{groupId}; 925 </select> 926 927 <select id="queryOrgVisitedRecordSize" resultType="int" 928 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition"> 929 select count(sub.id) from 930 (select 931 name, 932 id, 933 org_type, 934 tree_code 935 from qadmin_org where id = #{groupId}) 936 parent 937 left join 938 (select 939 name, 940 id, 941 org_type, 942 tree_code 943 from qadmin_org where org_type = #{dimension}) 944 sub 945 on sub.tree_code like concat(parent.tree_code, ‘%‘); 946 </select> 947 948 <select id="queryBDVisitedInMultiGroupRecordSize" resultType="int" 949 parameterType="com.qunar.hotel.crm.model.condition.VisitStatisticsCondition"> 950 select 951 count(sub.id) 952 from 953 (select id, name, tree_code from qadmin_org) 954 parent 955 left join 956 (select 957 org.name, 958 org.tree_code, 959 user.id, 960 user.realname 961 from 962 qadmin_org org inner join 963 qadmin_user user 964 on user.org_id = org.id) 965 sub 966 on sub.tree_code like concat(parent.tree_code, ‘%‘) 967 where 968 parent.id in 969 <foreach collection="groupIds" index="index" item="item" open="(" separator="," close=")"> 970 #{item} 971 </foreach> 972 </select> 973 974 </mapper>
花费了半天的分析后,重构代码,SQL压缩为96行
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="com.qunar.scm.visit.dao.VisitStatisticsDao"> 4 <resultMap id="visitStatisticsResult" type="com.qunar.scm.visit.model.vo.VisitStatisticsResult"/> 5 6 <sql id="order"> 7 <choose> 8 <when test="orderBy == ‘totalVisited‘"> 9 totalVisited 10 </when> 11 <when test="orderBy == ‘byDoorVisited‘"> 12 byDoorVisited 13 </when> 14 <when test="orderBy == ‘KPVisited‘"> 15 KPVisited 16 </when> 17 <when test="orderBy == ‘byDoorKPVisited‘"> 18 byDoorKPVisited 19 </when> 20 <when test="orderBy == ‘revisited‘"> 21 revisited 22 </when> 23 <otherwise> 24 totalVisited 25 </otherwise> 26 </choose> 27 <choose> 28 <when test="orderType == ‘asc‘"> 29 asc 30 </when> 31 <otherwise> 32 desc 33 </otherwise> 34 </choose> 35 </sql> 36 37 <sql id="where"> 38 <where> 39 visit.visit_date >= #{dateFrom} and #{dateTo} >= visit.visit_date 40 and visit.valid = 1 41 <if test="userId != null and userId != ‘‘ "> 42 and user_id = #{userId} 43 </if> 44 <if test="orgTreeCodes != null and orgTreeCodes.size >0 "> 45 AND 46 <foreach collection="orgTreeCodes" item="treeCode" open="(" close=")" separator="or"> 47 org_tree_code like ‘${treeCode}%‘ 48 </foreach> 49 </if> 50 </where> 51 </sql> 52 53 <sql id="column"> 54 user_id as ‘groupId‘, 55 count(1) as totalVisited, 56 sum(case when visit.visit_type =2 then 1 else 0 end) as byDoorVisited, 57 sum(case when visit.is_active = 0 then 1 else 0 end) as KPVisited, 58 sum(case when visit.is_active = 0 and visit.visit_type =2 then 1 else 0 end) as byDoorKPVisited, 59 sum(case when visit.revisit_valid = 1 then 1 else 0 end) as revisited, 60 sum(case when (visit.visit_purpose_type = ‘1‘ or visit.visit_purpose_type=‘1#2‘) then 1 else 0 end) as visitforSignCount, 61 sum(case when (visit.visit_purpose_type = ‘2‘ or visit.visit_purpose_type=‘1#2‘) then 1 else 0 end) as visitforMantainceCount 62 </sql> 63 64 65 <select id="queryBDVisitedCountInMultiGroupWithRowHandler" resultMap="visitStatisticsResult" 66 parameterType="com.qunar.scm.visit.model.condition.VisitStatisticsCondition" 67 fetchSize="100" resultSetType="FORWARD_ONLY"> 68 select 69 <include refid="column"/> 70 from crm_visit_record visit 71 <include refid="where"/> 72 group by user_id 73 order by 74 <include refid="order"/> 75 </select> 76 77 78 79 <select id="queryBDVisitedCountInMultiGroup" resultMap="visitStatisticsResult" 80 parameterType="com.qunar.scm.visit.model.condition.VisitStatisticsCondition"> 81 select 82 <include refid="column"/> 83 from crm_visit_record visit 84 <include refid="where"/> 85 group by user_id 86 order by 87 <include refid="order"/> 88 </select> 89 90 <select id="queryBDVisitedInMultiGroupRecordSize" resultType="int" 91 parameterType="com.qunar.scm.visit.model.condition.VisitStatisticsCondition"> 92 select 93 count(distinct user_id) 94 from crm_visit_record visit 95 <include refid="where"/> 96 </select> 97 98 </mapper>
简要分析:
整个SQL分类:
1 6个函数,count 3个,select list 3个,where条件分别为查单独一个人,查一个组织。6个函数完全可以压缩为2个函数。
2 原SQL原理
先生成多个统计指标的temp表,然后使用uid进行left join
select count(1) from A where W1 left join select count(1) from A where W2 left join select count(1) from A where W3 order by XXXXX
换一种思路 使用CASE WHEN代替即可
select SUM(case when W1 then 1 else 0 end), SUM(case when W2 then 1 else 0 end), SUM(case when W3 then 1 else 0 end) FROM A ORDER BY XXXX
挫代码处处有,也许一开始是不挫的,随着一代代不加思考叠加,最终就成了没法维护的代码。所以,对于Coding,无论何时,保持一个谨慎的态度。
标签:
原文地址:http://www.cnblogs.com/songxinya/p/5818591.html