2
INSERT INTO contacts_lists (contact_id, list_id)
    SELECT contact_id, 110689 AS list_id 
    FROM plain_contacts 
    WHERE TRUE 
        AND is_print = TRUE 
        AND ( ( TRUE 
                AND country_id IN (231,39) 
                AND company_type_id IN (2,8,12,5,6,4,3,9,10,13,11,1,7) 
                AND is_broadcast = TRUE ) 
            OR ( TRUE 
                AND country_id IN (15,59,73,74,81,108,155,165,204,210,211,230) 
                AND company_type_id IN (2,8,12,5,6,4,3,9,10,13,11,1,7) 
                AND is_broadcast = TRUE ) 
            OR ( TRUE 
                AND country_id IN (230) 
                AND company_type_id IN (2,8,12,5,6,4,3,9,10,13,11,1,7) 
                AND is_broadcast = TRUE )) 
        AND (NOT EXISTS (
        SELECT title_id 
            FROM company_types_lists_titles 
            WHERE company_types_list_id = 92080) 
            OR title_id IN (
        SELECT title_id 
            FROM company_types_lists_titles 
            WHERE company_types_list_id = 92080)) 
        AND company_type_id = 2 
        AND country_id IN (
    SELECT country_id 
        FROM countries_lists 
        WHERE list_id = 110689)
        AND ((state_id IS NULL 
                OR country_id NOT IN (231,39) 
                OR state_id IN (
            SELECT state_id 
                FROM lists_states 
                WHERE list_id = 110689))
            OR zone_ids && ARRAY(
        SELECT zone_id 
            FROM lists_zones 
            WHERE list_id = 110689)
    )
        AND (NOT EXISTS (
        SELECT award_id 
            FROM company_types_lists_top_awards 
            WHERE company_types_list_id = 92080) 
            OR top_award_ids && ARRAY(
        SELECT award_id 
            FROM company_types_lists_top_awards 
            WHERE company_types_list_id = 92080))  

我使用了 postgresql,它从各种表中选择 30000 行,从各种表中选择数据需要不到一秒钟的时间。但是在选择需要越来越多时间插入另一个表的数据之后。如何减少插入时间。这是我的查询。在这个选择查询中给出了近 3000 万条记录。

4

2 回答 2

1

需要越来越多的时间来插入

这通常意味着您缺少索引。


编辑:既然您已经发布了查询...肯定缺少一个或多个索引以加快插入过程中的查找速度。而且您可能想要重写那个巨大的选择语句以减少嵌套。

于 2011-07-28T11:25:36.970 回答
0

If no other people (threads) are meanwhile working with the target table, you could drop the indexes for the table, insert the data, and recreate the indexes later.

This may lead to a speed up, and might be considered, if your data is reliable, and you can guarantee, that you won't violate unique-restrictions.

于 2011-07-28T11:43:26.590 回答