1
mortonnex 2019-03-11 09:50:37 +08:00
explain 走一波
|
2
leonme 2019-03-11 09:53:49 +08:00 via Android 1
先从上层理解业务,然后在看具体 sql ~不理解业务,直接看 sql 是很痛苦的,而且还看不明白~其实这 sql 语句只是长一点,分解一下就懂了
|
4
sonyxperia 2019-03-11 09:55:36 +08:00
2l +1
|
5
ChiangDi 2019-03-11 10:10:29 +08:00
跑路
|
6
MINYAN 2019-03-11 10:21:49 +08:00
2L+2,建议格式化以后看~
|
7
cydleadingx 2019-03-11 10:26:36 +08:00
2L+3 建议找人讲讲,从需求业务触发
|
8
ghbaqi OP @MINYAN @cydleadingx 领导和我说之前这里外包做的 , 现在公司里没有一个人知道这块业务 ,也没有文档 。 要我主导 领导和我说 : “ 这块因为是从 0 到 1 的,公司没人了解,所以这次是希望你更多的主导的,后面这块业务你梳理好后,这块改动都要你来主导 ” 。。。。。。。
|
9
kamal 2019-03-11 10:42:45 +08:00 2
SELECT
b.secu_id AS f_secu_id, b.fund_code AS trd_code, b.fundfullname AS chi_name, b.fundname AS secu_sht, b.companyid AS fmc_com_id, b.companyname AS fmc_com_name, b.f_info_custodianbank AS cust_com_name, IF ( f_pchredm_pchstartdate IS NULL OR f_pchredm_pchstartdate > Date_format(Now(), '%Y%m%d') OR Locate('633001001', pur_st_temp) > 0, '暂停申购', IF( Locate('633001002', pur_st_temp) > 0, '暂停大额申购', IF( Locate('633001003', pur_st_temp) > 0, '暂停定期定额申购', IF( Locate('633001004', pur_st_temp) > 0, '暂停大额定期定额申购', '开放申购' ) ) ) ) AS pur_st, IF ( f_info_redmstartdate IS NULL OR f_info_redmstartdate > Date_format(Now(), '%Y%m%d') OR Locate('633001005', redem_st_temp) > 0, '暂停赎回', IF( Locate('633001006', pur_st_temp) > 0, '暂停实时赎回', '开放赎回' ) ) AS redem_st, b.isqdii AS is_qdii, b.fundtype AS inv_typ_com, ( CASE b.fundtype WHEN '10100' THEN '股票型' WHEN '10200' THEN '债券型' WHEN '10300' THEN '货币型' WHEN '10400' THEN '混合型' WHEN '10600' THEN 'FOF' WHEN '10700' THEN '保本型' WHEN '10800' THEN '理财型' WHEN '10901' THEN 'QDII' WHEN '10905' THEN '指数型' ELSE '专户' end ) AS inv_typ_com_desc, Date_format(c.trade_dt, '%Y-%m-%d') AS trd_dt, nav.f_nav_unit AS unit_nav, c.f_avgreturn_day AS chg_rat_1d, c.f_avgreturn_week AS chg_rat_1w, c.f_avgreturn_month AS chg_rat_1m, c.f_avgreturn_quarter AS chg_rat_3m, c.f_avgreturn_halfyear AS chg_rat_6m, c.f_avgreturn_year AS chg_rat_1y, c.f_avgreturn_twoyea AS chg_rat_2y, c.f_avgreturn_threeyear AS chg_rat_3y, c.f_avgreturn_fiveyear AS chg_rat_5y, '' AS chg_rat_10y, c.f_avgreturn_sincefound AS chg_rat_bgn, c.f_sfrank_day AS chg_rat_1d_rk, c.f_sfrank_recentweek AS chg_rat_1w_rk, c.f_sfrank_recentmonth AS chg_rat_1m_rk, c.f_sfrank_recentquarter AS chg_rat_3m_rk, c.f_sfrank_recenthalfyear AS chg_rat_6m_rk, c.f_sfrank_recentyear AS chg_rat_1y_rk, c.f_sfrank_recenttwoyear AS chg_rat_2y_rk, c.f_sfrank_recentthreeyear AS chg_rat_3y_rk, c.f_sfrank_recentfiveyear AS chg_rat_5y_rk, '' AS chg_rat_10y_rk, c.f_sfrank_sincefound AS chg_rat_bgn_rk, d.f_income_per_million AS unit_yld, d.f_info_yearlyroe AS ann_yld_rat FROM ( SELECT base.*, redm.pur_st_temp, redm.redem_st_temp FROM ( SELECT CASE wb.fundtype WHEN '10901' THEN '1' ELSE '0' end AS isqdii, f_info_custodianbank, f_pchredm_pchstartdate, f_info_redmstartdate, wb.companyid, wb.companyname, wb.fundfullname, wb.fundname, wb.fundtype, wb.fund_code, wb.secu_id FROM wb_fundinfo wb, wind_db.chinamutualfunddescription cmfd WHERE cmfd.f_info_windcode = wb.secu_id ) base LEFT JOIN ( SELECT s_info_windcode, Group_concat(DISTINCT f_info_suspchtype) AS pur_st_temp, Group_concat(DISTINCT f_info_suspchtype) AS redem_st_temp FROM wind_db.chinamutualfundsuspendpchredm WHERE s_info_windcode = #{trdcode} AND ( ( f_info_suspchstartdt <= date_format(now(), '%Y%m%d') AND f_info_repchdt IS NULL ) OR ( f_info_suspchstartdt <= date_format(now(), '%Y%m%d') AND f_info_repchdt > date_format(now(), '%Y%m%d') ) ) GROUP BY s_info_windcode ) redm ON base.secu_id = redm.s_info_windcode ) b LEFT JOIN wind_db.chinamfperformance_new c ON b.secu_id = c.s_info_windcode LEFT JOIN ( SELECT f_nav_unit, f_info_windcode, ann_date, price_date FROM wind_db.chinamutualfundnav_new cmfnav ) nav ON b.secu_id = nav.f_info_windcode LEFT JOIN wind_db.cmoneymarketdailyfincome d ON b.secu_id = d.s_info_windcode AND d.f_info_enddate = nav.price_date WHERE b.fund_code = #{trdcode} |
10
tiedan 2019-03-11 10:44:42 +08:00
我还见过上千行的存储过程
|
11
Shynoob 2019-03-11 10:55:50 +08:00
先明白这条 sql 的目的是什么,然后拆分子查询,把子查询都阅读明白,结合业务就比较好读懂了
|
12
sfz97308 2019-03-11 11:01:31 +08:00
见过比这还长好多的 SQL,来自印度...
|
13
reus 2019-03-11 11:03:25 +08:00
哪个公司啊?绝对不能在这里买基金啊!
|
14
wps353 2019-03-11 11:07:14 +08:00
这怕是属于 OLAP 的 SQL 了吧。。
|
16
Raisu 2019-03-11 11:48:02 +08:00 via Android
可怕
|
17
pan569673372 2019-03-11 13:30:30 +08:00 via Android
@tiedan 我还写过上千行的嘞,理解业务万行都好说
|
18
lazyfighter 2019-03-11 13:43:06 +08:00
这是报表吧 还好 报表 sql 都很复杂
|
19
lichungang 2019-03-11 13:51:03 +08:00
印度。。哈哈哈哈,为啥这么想笑
|
20
laidycy 2019-03-11 14:01:56 +08:00
恩,不是我们公司的 SQL
|
21
ghbaqi OP @lazyfighter 不是报表 业务代码
|
22
saulshao 2019-03-11 17:01:51 +08:00
这....厉害!这种代码貌似很多.....
我反正见过很多很多 |
23
e2c 2019-03-11 18:11:31 +08:00
存储过程写长点无所谓,但是一个查询语句搞这么长,是要整死接手的人吗
|
24
Leigg 2019-03-11 18:52:40 +08:00 via iPhone
有毒
|
25
shehuizhuyi 2019-03-11 19:23:44 +08:00 via iPhone
@kamal 这条 sql 执行后服务器不得爆炸
|
26
kangzai50136 2019-03-11 19:28:12 +08:00 via Android
厉害。
|
27
alakey1989 2019-03-11 19:45:54 +08:00
我尼玛~1
|
28
greed1is9good 2019-03-11 20:31:40 +08:00 via Android
还好,不是很长。。。
|
29
huobazi 2019-03-11 22:04:44 +08:00
正常了
|
30
akatquas 2019-03-11 22:13:27 +08:00 via iPhone
用你的语言习惯改写业务接口,同时读代码,理解 SQL 在干嘛就很容易。
顺便你完成了一次重构(狗头 |
31
ghbaqi OP @akatquas 关键就是不知道业务 , 公司也没人知道这块业务 , 要我自己看 ...................
|
32
Damon4V 2019-03-12 11:13:25 +08:00
重构吧
|