- 积分
- 53
- UID
- 5484
- 阅读权限
- 20
- 注册时间
- 2010-6-18
- 精华
- 在线时间
- 小时
- 最后登录
- 1970-1-1
- 职业
- 3
|
3#
楼主 |
发表于 2016-4-25 14:30:14
|
只看该作者
本帖最后由 ccxpts 于 2016-4-25 14:40 编辑
1-将18位的身份证号码转换为15位的号码
DELIMITER $
DROP FUNCTION IF EXISTS get_sfz$
CREATE DEFINER = CURRENT_USER FUNCTION get_sfz(
in_SFZ CHAR(32))
RETURNS CHAR(32)
NO SQL
BEGIN
DECLARE c_str CHAR(32) default '';
SET c_str = in_SFZ;
IF LENGTH(in_SFZ) = 18 THEN
SET c_str = concat( left(in_SFZ,6), substring(in_SFZ,9,9) );
END IF;
RETURN c_str;
END;$
DELIMITER ;
2-查询SQL
select a.* from (
select KHJLFLNM as xm, GSWDBRNO as jg, get_sfz( ZJHMCFNO ) as sfz1
from GLFMKHJL where length(KHJLSTAF)=7 and XTDLBOOL<>'9'
) as a
left join (
select CIP_CSTNO as khh1, get_sfz( CIP_CTFNO ) as sfz2
from IBS_PB_CSTINF_PRO
) as b on a.sfz1=b.sfz2;
---------------------------------------------------------------------------------------------
对SQL进行计划查看:
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: GLFMKHJL
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 900
filtered: 90.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: IBS_PB_CSTINF_PRO
partitions: NULL
type: index
possible_keys: NULL
key: sfz_idx
key_len: 63
ref: NULL
rows: 39411
filtered: 100.00
Extra: Using where; Using index; Using join buffer (Block Nested Loop)
2 rows in set, 1 warning (0.01 sec)
------------------------------------------------------------------------------------------------------------------
show warnings;
| Note | 1003 | /* select#1 */ select `xxgl2`.`GLFMKHJL`.`KHJLFLNM` AS `xm`,`xxgl2`.`GLFMKHJL`.`GSWDBRNO` AS `jg`,`get_sfz15to18`(`xxgl2`.`GLFMKHJL`.`ZJHMCFNO`) AS `sfz1` from `xxgl2`.`GLFMKHJL` left join (`xxgl2`.`IBS_PB_CSTINF_PRO`) on((`get_sfz15to18`(`xxgl2`.`GLFMKHJL`.`ZJHMCFNO`) = `get_sfz15to18`(`xxgl2`.`IBS_PB_CSTINF_PRO`.`CIP_CTFNO`))) where ((length(`xxgl2`.`GLFMKHJL`.`KHJLSTAF`) = 7) and (`xxgl2`.`GLFMKHJL`.`XTDLBOOL` <> '9')
从警告信息看,这条SQL被自动转换为:
select KHJLFLNM AS xm,GSWDBRNO AS jg, get_sfz15to18(ZJHMCFNO) AS sfz1
from GLFMKHJL
left join IBS_PB_CSTINF_PRO on get_sfz15to18(ZJHMCFNO) = get_sfz15to18(CIP_CTFNO)
where length(KHJLSTAF) = 7 and XTDLBOOL<>'9'
执行转换后我的这条SQL很慢,需25分以上
-------------------------------------------------------------------------------------------------------------------
上述SQL在5.5环境下进行计划查看:
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 668
Extra:
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: <derived3>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 39803
Extra:
*************************** 3. row ***************************
id: 3
select_type: DERIVED
table: IBS_PB_CSTINF_PRO
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 39803
Extra:
*************************** 4. row ***************************
id: 2
select_type: DERIVED
table: GLFMKHJL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 905
Extra: Using where
|
|