How can my application benefit from temporary tables?

后端 未结 6 1622
一生所求
一生所求 2020-12-31 06:07

I\'ve been reading a little about temporary tables in MySQL but I\'m an admitted newbie when it comes to databases in general and MySQL in particular. I\'ve looked at some

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-31 06:21

    Temporary tables are often valuable when you have a fairly complicated SELECT you want to perform and then perform a bunch of queries on that...

    You can do something like:

    
    CREATE TEMPORARY TABLE myTopCustomers
       SELECT customers.*,count(*) num from customers join purchases using(customerID)
       join items using(itemID) GROUP BY customers.ID HAVING num > 10;
    

    And then do a bunch of queries against myTopCustomers without having to do the joins to purchases and items on each query. Then when your application no longer needs the database handle, no cleanup needs to be done.

    Almost always you'll see temporary tables used for derived tables that were expensive to create.

提交回复
热议问题