Search code examples
mariadbxwiki

Deleting a 3GB table took over two hours on MariaDB


As posted on another forum, when upgrading XWiki from v7.0.1 to v13.10.9, a non-critical database table xwikistatsvisit, for the user visiting statistics, was preventing the after-upgrade migrations. It contained over seven million records and sized 3GB in total. As a workaround, we had to delete all records in the table, but the SQL command delete from table xwikistatsvisit took over two hours.

I have verified from ER diagram that the table is stand-alone without any foreign key referring to or from other tables. And the database is MariaDB v10.9.2 installed on the same host.

The host under test is a medium virtual machine with SSD, 4 CPUs of Intel i9 and 8GB of RAM, running MariaDB v10.9.2. Also, the hypervisor needs to enable “PAE/NX” and “Nested VT-x/ADM-V” for higher performance; otherwise, the computing task will get stuck forever.

My Questions:

Why the SQL command took so long? Is there any way to proceed faster? E.g. disabling the keys and restrictions, etc.; but I am unfamiliar with this area.

I will highly appreciate any hints or suggestions.

The definition of the table xwikistatsvisit:

--
-- Table structure for table `xwikistatsvisit`
--

DROP TABLE IF EXISTS `xwikistatsvisit`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `xwikistatsvisit` (
  `XWV_ID` bigint(20) NOT NULL,
  `XWV_NUMBER` int(11) DEFAULT NULL,
  `XWV_NAME` varchar(255) NOT NULL,
  `XWV_CLASSNAME` varchar(255) DEFAULT NULL,
  `XWV_IP` varchar(255) NOT NULL,
  `XWV_USER_AGENT` longtext NOT NULL,
  `XWV_COOKIE` longtext NOT NULL,
  `XWV_UNIQUE_ID` varchar(255) NOT NULL,
  `XWV_PAGE_VIEWS` int(11) DEFAULT NULL,
  `XWV_PAGE_SAVES` int(11) DEFAULT NULL,
  `XWV_DOWNLOADS` int(11) DEFAULT NULL,
  `XWV_START_DATE` datetime DEFAULT NULL,
  `XWV_END_DATE` datetime DEFAULT NULL,
  PRIMARY KEY (`XWV_ID`),
  KEY `XWVS_END_DATE` (`XWV_END_DATE`),
  KEY `XWVS_UNIQUE_ID` (`XWV_UNIQUE_ID`),
  KEY `XWVS_PAGE_VIEWS` (`XWV_PAGE_VIEWS`),
  KEY `XWVS_START_DATE` (`XWV_START_DATE`),
  KEY `XWVS_NAME` (`XWV_NAME`),
  KEY `XWVS_PAGE_SAVES` (`XWV_PAGE_SAVES`),
  KEY `XWVS_DOWNLOADS` (`XWV_DOWNLOADS`),
  KEY `XWVS_IP` (`XWV_IP`),
  KEY `xwv_user_agent` (`XWV_USER_AGENT`(255)),
  KEY `xwv_classname` (`XWV_CLASSNAME`),
  KEY `xwv_number` (`XWV_NUMBER`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

Solution

  • The DELETE command deletes all the matching rows one at a time. If you want to delete all the records from such a big table, TRUNCATE should be a lot faster, since it empties the full table at once.

    TRUNCATE TABLE xwikistatsvisit;
    

    Difference between DELETE and TRUNCATE.