How can my application benefit from temporary tables?

后端 未结 6 1627
一生所求
一生所求 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:39

    I haven't done them in MySQL, but I've done them on other databases (Oracle, SQL Server, etc).

    Among other tasks, temporary tables provide a way for you to create a queryable (and returnable, say from a sproc) dataset that's purpose-built. Let's say you have several tables of figures -- you can use a temporary table to roll those figures up to nice, clean totals (or other math), then join that temp table to others in your schema for final output. (An example of this, in one of my projects, is calculating how many scheduled calls a given sales-related employee must make per week, bi-weekly, monthly, etc.)

    I also often use them as a means of "tilting" the data -- turning columns to rows, etc. They're good for advanced data processing -- but only use them when you need to. (My golden rule, as always, applies: If you don't know why you're using x, and you don't know how x works, then you probably shouldn't use it.)

    Generally, I wind up using them most in sprocs, where complex data processing is needed. I'd love to give a concrete example, but mine would be in T-SQL (as opposed to MySQL's more standard SQL), and also they're all client/production code which I can't share. I'm sure someone else here on SO will pick up and provide some genuine sample code; this was just to help you get the gist of what problem domain temp tables address.

提交回复
热议问题