Pass index to temporary table from regular table?

前端 未结 2 1216
暖寄归人
暖寄归人 2021-01-06 00:45

I am creating a temp table with a query like this:

CREATE TEMPORARY TABLE temp_table
SELECT * FROM regular_table
WHERE 1

But regular_table

2条回答
  •  别那么骄傲
    2021-01-06 01:31

    You could use CREATE TEMPORARY TABLE temp_table LIKE regular_table, but that will create all the indexes, so when you do INSERT INTO temp_table SELECT * FROM regular_table, the indexes will be rebuilt - which could be lengthy.

    Or, you can create the table and add the index afterwards:

    CREATE TEMPORARY TABLE temp_table
    ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
    INSERT INTO temp_table SELECT * FROM regular_table
    

    but the index will be, again, updated on every insert.

    Probably the most efficient way would be to create the temp table, insert all, build index afterwards:

    CREATE TEMPORARY TABLE temp_table
    ALTER TABLE temp_table ADD FULLTEXT INDEX (foo,bar,baz)
    ALTER TABLE temp_table DISABLE KEYS
    INSERT INTO temp_table SELECT * FROM regular_table
    ALTER TABLE temp_table ENABLE KEYS
    

    Again, you will have to wait for the index to build, except it will happen in one chunk, with the last ALTER statement.

提交回复
热议问题