Comments (16)
It is quite convenient for some big data tables where we rotate data by monthly basis. For e.g, I usually have to set the following commands to run in monthly basis for big tables (several million rows):
CREATE TABLE log_events201609 LIKE log_events;
INSERT INTO log_events201609 SELECT * FROM log_events WHERE event_date < '2016-09-01';
DELETE FROM log_events WHERE event_date < '2016-09-01';
I know there is table partitioning in all DB engine, but it quite dangerous to group all the important logging data into one table. Also, after soft-partitioning the data like that, we can easily export the whole monthly report to flat files for reference later.
from db.
backup history data before data migration
from db.
Might be implemented.
from db.
I usually do something like CREATE TABLE copy AS SELECT * FROM original
but that doesn't include table constraints. If you add LIMIT 0
you only copy the structure, without the data.
from db.
What's the use case?
from db.
@nineinchnick CREATE TABLE copy AS SELECT * FROM original
that's useful on mysql platform but not well on mssql
from db.
Useful for what?
from db.
@samdark copyTable
from db.
The question here is: Why would you ever want to copy a table?
from db.
The question here is: Why would you ever want to copy a table?
As both copying & tables are quite generic, you are ignoring users creativity in general with this question.
In fact, these are so generic that there will be infinite usecases.
Therefore 'why' is not the question, but 'how'.
from db.
Here is the usecase: I've got a huge table to be updated from the external source (for example: geo blocks), it may be significantly changed so to actually update is unnecessary hard.
So I should do:
- copy table structure (without index keys)
- fill the new temp table with actual data
- create index keys
- drop the old table
- rename the new table
Everything but the first is perfectly done with createCommand()
. And it is rather complicated, because I should check if the temp table already exists, truncate it or drop and create. And I'm forced to either store the table definition in two places (migration and data update) or use a cheat like CREATE TABLE ... AS SELECT * FROM ... LIMIT 0
Actually the copyTable()
method should not be that hard to implement. We've got the yii\db\Connection::getTableSchema()
method and yii\db\TableSchema::$columns
property, so the main question is how to force createTable()
to use the array of yii\db\ColumnSchema
and that will do.
from db.
Note that table schema is usually cached.
I'm OK having a method but this way of updating data doesn't sound familiar to me. What are pros compared to simple update in this case? Also I guess original table should be locked during this process else you're going to lose data written into original table during it.
from db.
In my case it is the geo blocks table (about 3M of rows) and the key is start_ip
-finish_ip
. There may be (and there are) different keys in the update data. The IP ranges may match, intersect, separate or differ in other ways. It is possible to find and update (actually delete and insert I think) ranges but it is a bit harder then to create new table and replace the old one after data collection.
The second moment (in this particular case) is that the update/insert operations on the table with 3M rows of data and indices is very slow (in terms of DB). It is much faster to insert in empty table without indices and then to create indices. It is very useful approach for the dictionary-like data tables but you are to be sure about the input data (duplicates etc.). The faster update, instantly replaced table so no locks, no read lags.
And in my case the table is "read-only" after the update so I do not need to lock it as there are no any writes to the table. It is a rare case, but it happens...
from db.
Have you tried SET foreign_key_checks = 0;
, SET unique_checks = 0;
etc.?
from db.
Nope. But even if it fixes the data insertion timing problem, you still have to fight the search/replace ranges problem...
In other words everything is possible but is it worth it?!
Anyway it is just a usecase (for copyTable()), I think one of many...
from db.
OK.
from db.
Related Issues (20)
- Inconsistency of `Schema::getTableForeignKeys()` HOT 2
- Unique indexes can be loaded in 3 different ways HOT 1
- Make array type only for `Constraint::$columnNames`
- Support enum values by all DBMS
- `Query::count()` can return `int|string` but always returns `int` HOT 1
- `Query::leftJoin` - a simpler way to quote join column? HOT 3
- Remove `$withColumn` parameter from `getTableNameParts()` HOT 3
- Remove method `TableSchema::compositeForeignKey()` HOT 1
- `CommandInterface::getRawSQL()` - using in production / alternative HOT 12
- `Schema::hasTableName()` method is missing HOT 1
- Allow scalars `int|float|bool` in `Query::select()` HOT 1
- Changes before release 2.0.0
- Support Query result typing HOT 13
- Allow use `DMLQueryBuilderInterface::batchInsert()` without `$columns` HOT 4
- Add posibility to drop table `IF EXISTS` and `CASCADE`
- Allow pass `ExpressionInterface` as alias to `QueryPartsInterface::withQuery()` HOT 1
- No usage of `AbstractCommand::requireTransaction()` HOT 3
- Needs to refactor `getRawSql()`
- Setting escapingReplacements property of the LikeCondition HOT 2
- Bug in `Command::insertWithReturningPks()` with empty values
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from db.