# Insert Ignore into table takes a long time

**URL:** <https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545>\
**Category:** Procedural SQL\
**Created:** [November 20, 2021, 2:11am UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545 "2021-11-20T02:11:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DataSleek](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/datasleek/32/402_2.png) [@DataSleek](https://www.singlestore.com/forum/u/DataSleek)\
**Post date:** [November 20, 2021, 2:11am UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/1 "2021-11-20T02:11:20Z")

</div>

Hi,

It takes more than 2 min to insert 200k rows from the query below.  
When it’s less than 100k rows, it’s much faster.

```
Insert Ignore into table (col1, col2, col3 .. col8
)

Select col1, col2, col3 .. col8
FROM another table  
where TStampInserted between v_start_epoc AND v_epoc_now
AND TStampTraded <= (v_epoc_now - epoc_engine)
```

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [November 22, 2021, 11:35pm UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/2 "2021-11-22T23:35:43Z")

</div>

How long exactly, and how many rows exactly?

Did the PROFILE query plan look different in each case?

How big was the source table?

---

<div class="post-metadata">

**Author:** ![DataSleek](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/datasleek/32/402_2.png) [@DataSleek](https://www.singlestore.com/forum/u/DataSleek)\
**Post date:** [November 24, 2021, 5:06pm UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/3 "2021-11-24T17:06:59Z")

</div>

Hi @hanson ,

Thank you for looking into this.  
I’m trying to merge data from different period into a single table. The destination table (_SpotTrade\_TS\_History_) has data being inserted into every 5 min. The source table i’m getting data from has 945,130,366 rows. As you can see below i’m controlling number of rows based on time ranges.  
I had done some test on _SpotTrade\_TS\_History_ table which is supposed to be the final destination table and it was struggling with 200k inserts. So i decided to create a separate table _SpotTrade\_TS\_2021\_10_ (with no data in it) and insert into it. As you can see it takes 10 min to insert 75,500,543 rows.

We’re using a S4.

```
Insert ignore into SpotTrade_TS_2021_10 (ExchangeTradeId , TokenId , Price , Size , Pair , Side , ExchangeId ,TStampTraded,TSTradeDate,TStampEdited,TStampInserted) Select ExchangeTradeId , TokenId , Price , Size , Pair , Side , ExchangeId ,TStampTraded ,from_unixtime(TStampTraded) as TSTradeDate,TStampEdited ,TStampInserted FROM SpotTrade_Archive_202110 where TStampTraded >= UNIX_TIMESTAMP(date_format(date_add('2021-10-01 10:00:00', interval 13 day), '%Y-%m-%d 00:00:00')) and TStampTraded < UNIX_TIMESTAMP(date_format(date_add('2021-10-01 10:00:00', interval 13+1 day), '%Y-%m-%d 00:00:00'))
--------------

Query OK, 59321011 rows affected (10 min 59.56 sec)
Records: 0 Duplicates: 0 Warnings: 0

Bye
11/21 21:52:34 Import day
11/21 21:52:34 Starting import day
--------------
Insert ignore into SpotTrade_TS_2021_10 (ExchangeTradeId , TokenId , Price , Size , Pair , Side , ExchangeId ,TStampTraded,TSTradeDate,TStampEdited,TStampInserted) Select ExchangeTradeId , TokenId , Price , Size , Pair , Side , ExchangeId ,TStampTraded ,from_unixtime(TStampTraded) as TSTradeDate,TStampEdited ,TStampInserted FROM SpotTrade_Archive_202110 where TStampTraded >= UNIX_TIMESTAMP(date_format(date_add('2021-10-01 10:00:00', interval 14 day), '%Y-%m-%d 00:00:00')) and TStampTraded < UNIX_TIMESTAMP(date_format(date_add('2021-10-01 10:00:00', interval 14+1 day), '%Y-%m-%d 00:00:00'))
--------------

Query OK, 75500543 rows affected (9 min 23.41 sec)
Records: 0 Duplicates: 0 Warnings: 0

```

CREATE TABLE `SpotTrade_TS_2021_10` (  
`ExchangeTradeId` varchar(30) CHARACTER SET utf8 COLLATE utf8\_general\_ci NOT NULL,  
`TokenId` int(11) NOT NULL,  
`Price` decimal(20,10) NOT NULL,  
`Size` decimal(20,10) NOT NULL,  
`Pair` varchar(15) CHARACTER SET utf8 COLLATE utf8\_general\_ci NOT NULL,  
`Side` varchar(15) CHARACTER SET utf8 COLLATE utf8\_general\_ci DEFAULT NULL,  
`ExchangeId` int(11) NOT NULL,  
`TStampTraded` decimal(25,10) NOT NULL,  
`TSTradeDate` datetime(6) DEFAULT NULL SERIES TIMESTAMP,  
`TStampEdited` decimal(25,10) DEFAULT NULL,  
`TStampInserted` decimal(25,10) NOT NULL,  
KEY `TStampTraded` (`TStampTraded`) USING CLUSTERED COLUMNSTORE,  
SHARD KEY `idx_SHARDKEY` (`TokenId`,`ExchangeId`),  
KEY `idx_TokenId` (`TokenId`) USING HASH,  
KEY `idx_ExchangeId` (`ExchangeId`) USING HASH,  
KEY `idx_Price` (`Price`) USING HASH,  
KEY `idx_Size` (`Size`) USING HASH,  
KEY `idx_Pair` (`Pair`) USING HASH,  
KEY `idx_TSTradeDate` (`TSTradeDate`) USING HASH,  
UNIQUE KEY `TStampTraded_2` (`TStampTraded`,`TokenId`,`ExchangeId`,`Pair`,`Size`,`Price`,`ExchangeTradeId`) USING HASH  
) AUTOSTATS\_CARDINALITY\_MODE=INCREMENTAL AUTOSTATS\_HISTOGRAM\_MODE=CREATE AUTOSTATS\_SAMPLING=ON SQL\_MODE=‘STRICT\_ALL\_TABLES’

SpotTrade\_Archive\_202110  
Create Table: CREATE TABLE `SpotTrade_Archive_202110` (  
`ExchangeTradeId` varchar(30) CHARACTER SET utf8 COLLATE utf8\_general\_ci NOT NULL,  
`TokenId` int(11) NOT NULL,  
`Price` decimal(20,10) NOT NULL,  
`Size` decimal(20,10) NOT NULL,  
`Pair` varchar(15) CHARACTER SET utf8 COLLATE utf8\_general\_ci NOT NULL,  
`Side` varchar(15) CHARACTER SET utf8 COLLATE utf8\_general\_ci DEFAULT NULL,  
`ExchangeId` int(11) NOT NULL,  
`TStampTraded` decimal(25,10) NOT NULL,  
`TStampEdited` decimal(25,10) DEFAULT NULL,  
`TStampInserted` decimal(25,10) NOT NULL,  
KEY `TStampTraded` (`TStampTraded`) USING CLUSTERED COLUMNSTORE,  
SHARD KEY `idx_SHARDKEY` (`TStampTraded`,`TokenId`,`ExchangeId`,`Pair`,`Size`,`Price`,`ExchangeTradeId`),  
KEY `idx_TokenId` (`TokenId`) USING HASH,  
KEY `idx_ExchangeId` (`ExchangeId`) USING HASH  
) AUTOSTATS\_CARDINALITY\_MODE=INCREMENTAL AUTOSTATS\_HISTOGRAM\_MODE=CREATE AUTOSTATS\_SAMPLING=ON SQL\_MODE=‘STRICT\_ALL\_TABLES’

---

<div class="post-metadata">

**Author:** ![hanson](https://www.singlestore.com/forum/user_avatar/www.singlestore.com/hanson/32/196_2.png) [@hanson](https://www.singlestore.com/forum/u/hanson)\
**Post date:** [November 30, 2021, 12:54am UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/4 "2021-11-30T00:54:06Z")

</div>

Got it. Looks like you found a solution, then.

Using visual profile in our portal or in Studio is a good way to see differences between query plans, which could help debug situations like this in the future.

---

<div class="post-metadata">

**Author:** ![odinshaniya761](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/o/e9bcb4/32.png) [@odinshaniya761](https://www.singlestore.com/forum/u/odinshaniya761)\
**Post date:** [December 20, 2021, 6:38am UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/5 "2021-12-20T06:38:41Z")

</div>

Similar to deletes and replace into, with this scheme, “insert ignore” can be **two orders of magnitude faster than insertions into** a B-tree.

---

<div class="post-metadata">

**Author:** ![odinshaniya761](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/o/e9bcb4/32.png) [@odinshaniya761](https://www.singlestore.com/forum/u/odinshaniya761)\
**Post date:** [December 21, 2021, 6:43am UTC](https://www.singlestore.com/forum/t/insert-ignore-into-table-takes-a-long-time/3545/6 "2021-12-21T06:43:24Z")

</div>

Similar to deletes and replace into, with this scheme, “insert ignore” can be **two orders of magnitude faster than insertions into** a B-tree[.](https://www.liteblue.life/)
