PostgreSql 的hash_code 函数

本文详细介绍了在PostgreSQL中实现的hash_code函数,该函数与Java的hashCode方法一致,通过循环遍历输入字符串的每个字符并进行特定运算,最终返回一个整数哈希值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

PostgreSql 实现的hash_code 函数与java hash_code方法一致

CREATE FUNCTION hash_code(text) RETURNS integer
    LANGUAGE plpgsql
AS
$$
DECLARE
    i integer := 0;
    DECLARE
    h bigint  := 0;
BEGIN
    FOR i IN 1..length($1)
        LOOP
            h = (h * 31 + ascii(substring($1, i, 1))) & 4294967295;
        END LOOP;
    RETURN cast(cast(h AS bit(32)) AS int4);
END;
$$;
SQL: SELECT DISTINCT te3.store_code , ‘999999’ , CASE WHEN gl3.gondola_furniture_type IS NULL THEN NULL ELSE LPAD(TRIM(gl3.gondola_furniture_type), 3, ‘0’) END AS gondola_furniture_type , tt2.display_gondola , tt2.display_shelf , NULL :: numeric AS display_row , NULL :: numeric AS display_face , tt2.special_display_input_datetime , ‘2025-07-29’ :: date , CURRENT_TIMESTAMP , ‘SORD101011’ , ‘SORD101011’ , 0 FROM m_store_number AS te3 INNER JOIN m_specific_display_order_by_specific_category AS tt2 ON te3.original_store_code = tt2.original_store_code LEFT JOIN m_gondola_layout AS gl3 ON gl3.version = ‘0101_001’ AND tt2.original_store_code = gl3.original_store_code AND tt2.display_gondola = gl3.gondola_number INNER JOIN m_recommendation_group_by_specific_category AS rg ON rg.version = ‘0101_001’ AND tt2.original_store_code = rg.original_store_code AND ‘2025-07-29’ :: date BETWEEN rg.apply_start_date AND rg.apply_end_date INNER JOIN m_item_by_specific_category AS sh2 ON sh2.version = ‘0101_001’ AND rg.recommendation_group_type = sh2.recommendation_group_type AND rg.recommendation_group_code = sh2.recommendation_group_code AND ‘2025-07-29’ :: date BETWEEN sh2.apply_start_date AND sh2.apply_end_date AND tt2.information_category_code = sh2.information_category_code LEFT JOIN m_license AS li2 ON li2.version = ‘0101_001’ AND te3.original_store_code = li2.original_store_code AND sh2.license_code = li2.license_code WHERE te3.version = ‘0729_001’ AND ‘2025-07-29’ :: date BETWEEN te3.apply_start_date AND te3.apply_end_date AND ( sh2.license_code = ‘00’ OR ( sh2.license_code != ‘00’ AND li2.licenseditem_adopt_flag != ‘2’ ) ) 执行计划:QUERY PLAN Unique (cost=681.21…681.30 rows=6 width=233) (actual time=7668.496…8140.028 rows=1000 loops=1) -> Sort (cost=681.21…681.22 rows=6 width=233) (actual time=7668.494…7861.202 rows=1000000 loops=1) Sort Key: te3.store_code, (CASE WHEN (gondola_furniture_type IS NULL) THEN NULL::text ELSE lpad(TRIM(BOTH FROM gondola_furniture_type), 3, ‘0’::text) END), tt2.display_gondola, tt2.display_shelf, tt2.special_display_input_datetime Sort Method: external merge Disk: 91912kB -> Hash Left Join (cost=651.22…681.13 rows=6 width=233) (actual time=3.446…6537.932 rows=1000000 loops=1) Hash Cond: (((te3.original_store_code)::text = (original_store_code)::text) AND ((sh2.license_code)::text = (license_code)::text)) Filter: (((sh2.license_code)::text = ‘00’::text) OR (((sh2.license_code)::text <> ‘00’::text) AND ((licenseditem_adopt_flag)::text <> ‘2’::text))) -> Nested Loop (cost=651.22…681.02 rows=6 width=35) (actual time=3.429…6109.703 rows=1000000 loops=1) -> Nested Loop Left Join (cost=650.93…679.15 rows=1 width=35) (actual time=3.401…2633.210 rows=1000000 loops=1) Join Filter: (((tt2.original_store_code)::text = (original_store_code)::text) AND (tt2.display_gondola = gondola_number)) -> Nested Loop (cost=650.93…679.13 rows=1 width=35) (actual time=3.399…2370.852 rows=1000000 loops=1) Join Filter: ((sh2.information_category_code)::text = (tt2.information_category_code)::text) -> Merge Join (cost=650.66…665.91 rows=25 width=15) (actual time=3.360…308.377 rows=1000000 loops=1) Merge Cond: (((rg.recommendation_group_type)::text = (sh2.recommendation_group_type)::text) AND ((rg.recommendation_group_code)::text = (sh2.recommendation_group_code)::text)) -> Sort (cost=171.33…173.83 rows=1000 width=13) (actual time=1.449…1.612 rows=1000 loops=1) Sort Key: rg.recommendation_group_type, rg.recommendation_group_code Sort Method: quicksort Memory: 71kB -> Seq Scan on m_recommendation_group_by_specific_category_scncs3 rg (cost=0.00…121.50 rows=1000 width=13) (actual time=0.152…0.657 rows=1000 loops=1) Filter: ((‘2025-07-29’::date >= apply_start_date) AND (‘2025-07-29’::date <= apply_end_date) AND ((version)::text = ‘0101_001’::text)) -> Sort (cost=479.33…481.83 rows=1000 width=14) (actual time=1.903…70.010 rows=999001 loops=1) Sort Key: sh2.recommendation_group_type, sh2.recommendation_group_code Sort Method: quicksort Memory: 71kB -> Seq Scan on m_item_by_specific_category_scncs3 sh2 (cost=0.00…429.50 rows=1000 width=14) (actual time=0.412…1.207 rows=1000 loops=1) Filter: ((‘2025-07-29’::date >= apply_start_date) AND (‘2025-07-29’::date <= apply_end_date) AND ((version)::text = ‘0101_001’::text)) -> Index Scan using m_specific_display_order_by_specific_category_pkey on m_specific_display_order_by_specific_category tt2 (cost=0.28…0.52 rows=1 width=30) (actual time=0.001…0.002 rows=1 loops=1000000) Index Cond: ((original_store_code)::text = (rg.original_store_code)::text) -> Result (cost=0.00…0.00 rows=0 width=0) (actual time=0.000…0.000 rows=0 loops=1000000) One-Time Filter: false -> Index Scan using m_store_number_scncs3_pkey on m_store_number_scncs3 te3 (cost=0.29…1.87 rows=1 width=14) (actual time=0.003…0.003 rows=1 loops=1000000) Index Cond: (((version)::text = ‘0729_001’::text) AND ((original_store_code)::text = (tt2.original_store_code)::text) AND (apply_start_date <= ‘2025-07-29’::date)) Filter: (‘2025-07-29’::date <= apply_end_date) -> Hash (cost=0.00…0.00 rows=0 width=0) (actual time=0.001…0.002 rows=0 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 8kB -> Result (cost=0.00…0.00 rows=0 width=0) (actual time=0.000…0.001 rows=0 loops=1) One-Time Filter: false Planning Time: 5.648 ms Execution Time: 8257.282 ms 怎么优化
最新发布
08-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值