SQL Server replication drop vs truncate of target tables - sql-server

The sp_addarticle stored procedure, used to create publication articles in SQL Server speak, has a parameter #pre_creation_cmd through which dropping the table or truncating data at the target server can be specified for a snapshot. What circumstances are appropriate for each?

#pre_creation_cmd accepts one of four values:
NONE
DELETE
DROP
TRUNCATE
DELETE TABLE
Assume that your Published article
data is filtered and that the
corresponding table in the
Subscription receives data from other
sources, perhaps another Publication
for example. Using the DELETE
operation for this parameter would
delete "only" the data the meets the
criteria of the filter definition,
leaving the remaining data at the
Subscriber intact.
TRUNCATE TABLE
Removes all rows from a table without
logging the individual row deletions.
TRUNCATE TABLE is similar to the
DELETE statement with no WHERE clause;
however, TRUNCATE TABLE is faster and
uses fewer system and transaction log
resources.
DROP TABLE
Removes one or more table definitions
and all data, indexes, triggers,
constraints, and permission
specifications for those tables.

Related

Last modification time for table DDL metadata

Is there a system view in SAP HANA that contains last modification time for a table?
The time should contain timestamp when table DDL was changed, so I am interested particularly in table metadata last modification time.
For example I'd like to know when a column was added to the table or when a column was removed from the table.
The question isn't about SYS.M_TABLE_STATISTICS that contains information about last DML statement for the table.
You can use new system view M_EXECUTED_STATEMENTS. It monitors all DDL changes to the tables including:
All SQL statements starting with CREATE, DROP, ALTER, and RENAME. For example, CREATE TABLE, CREATE USER and ALTER TABLE.
All SQL statements starting with TRUNCATE, GRANT, REVOKE, LOAD, EXPORT, IMPORT, and COMMENT.
The SET SYSTEM LICENSE and UNSET SYSTEM LICENSE ALL statements.
It is enabled by default globally and was added in HANA SPS 11 exactly for that aim, i.e. for tracking invalid metadata issues.

Partition existing tables using PostgreSQL 10

I have gone through a bunch of documentation for PostgresSQL 10 partitioning but I am still not clear on whether existing tables can be partitioned. Most of the posts mention about partitioning existing tables using PostgreSQL 9.
Also, in the official PostgresSQL website : https://www.postgresql.org/docs/current/static/ddl-partitioning.html, it mentions 'It is not possible to turn a regular table into a partitioned table or vice versa'.
So, my question is can existing tables be partitioned in PostgreSQL 10?
If the answer is YES, my plan is :
Create a partitions
Alter the existing table to include the range so new data goes into the new partition. Once that is done, write a script which loops over the master table and moves the data into the right partitions.
Then, truncate the master table and enforce that nothing can be inserted into it.
If the answer is NO, my plan is to make the existing table the first partition
Create a new parent table and children(partitions).
Perform light transaction which will rename the existing table to a partition table name and the new parent to the actual table name.
Are there better ways to partition existing tables in PostgreSQL 10/9?

Changing columns to identity (SQL Server)

My company has an application with a bunch of database tables that used to use a sequence table to determine the next value to use. Recently, we switched this to using an identity property. The problem is that in order to upgrade a client to the latest version of the software, we have to change about 150 tables to identity. To do this manually, you can right click on a table, choose design, change (Is Identity) to "Yes" and then save the table. From what I understand, in the background, SQL Server exports this to a temporary table, drops the table and then copies everything back into the new table. Clients may have their own unique indexes and possibly other things specific to the client, so making a generic script isn't really an option.
It would be really awesome if there was a stored procedure for scripting this task rather than doing it in the GUI (which takes FOREVER). We made a macro that can go through and do this, but even then, it takes a long time to run and is error prone. Something like: exec sp_change_to_identity 'table_name', 'column name'
Does something like this exist? If not, how would you handle this situation?
Update: This is SQL Server 2008 R2.
This is what SSMS seems to do:
Obtain and Drop all the foreign keys pointing to the original table.
Obtain the Indexes, Triggers, Foreign Keys and Statistics of the original table.
Create a temp_table with the same schema as the original table, with the Identity field.
Insert into temp_table all the rows from the original table (Identity_Insert On).
Drop the original table (this will drop its indexes, triggers, foreign keys and statistics)
Rename temp_table to the original table name
Recreate the foreign keys obtained in (1)
Recreate the objects obtained in (2)

SQL Server : alter table A while table B performing CRUD?

Should I break my database up into two under this scenario?
Scenario
While customers are creating, editing, saving orders in the Order table, the website owner calls a stored procedure to alter table properties in the Email table.
Update
The only relation the Order table has with the Email table is the userId (FK on Email table) . So, what are the ramifications if WHILE customer is placing an order, I am simultaneous adding say a nullable "CcAddressId" column to the Email table. Would problems occur with this order being successful?
Questions:
Will either have a potential error if these events occurring simultaneously?
Would it be better to break up the database into groups?
Short answer is not to break the database in two because you will lose the referential integrity enforced by your foreign keys, not to mention other issues that others brought up above.
What you need to do is find out if SQL Server will issue any kind of lock for your EMails table as you are inserting data into Orders table.
You can experiment and find out for yourself using this as a starting point:
http://aboutsqlserver.com/2012/04/05/locking-in-microsoft-sql-server-part-13-schema-locks/
Altering a table will try to issue a schema modification lock (SCH-M) which is incompatible with any other kinds of locks. Therefore, if there is any activity going on over the table being altered (which I assume there will be because foreign key constraints are being validated), your schema modification statement will be blocked for a long time.
This is why it is better to run schema altering statements when your database is NOT under heavy load.

are there any options for doing bulk-insert to multiple related tables with entity framework (sql server 2008 r2 target)?

There are existing options for doing bulk insert into a single table with EF entities. Specifically, this SO question and using this class from David Browne.
In the case of trying to bulk insert rows into both a parent and child table, however, nothing jumps out as an option at that same level of convenience.
The 'hacks' I can think of (but I'm hoping there's at least one better option out there) include:
generate the PK's and set the FK's before insert (in this scenario, we know nothing else is inserting at the same time), then do the bulk inserts of both (turning off IDENTITY_INSERT during the parent insert if necessary)
bulk insert (using the linked SO question's approach) the parent rows, select them (enough columns to identify which parent row is which), generate child rows, bulk insert those
generate the sql necessary to insert all the rows in a single batch, doing each parent and then all related children, using ##identity to fill in the FK for the child inserts
The 'pregenerate PK values' approach (I haven't actually tried it) seems fine, but is fragile (requires no other inserts to at least parent during the operation) and depends on either an empty table or selecting max(pk)+1 beforehand.
Since SqlBulkCopy seems to be built around inserting a table at a time (like bcp), anything that still lets sql generate the PK/identity column would seem to be built around 'dropping down' to ado.net and building the sql.
Is there an option outside of 'generate the tons of sql' that I'm missing? If not, is there something out there that already generates the sql for mass-insert into related tables?
Thanks!!
The first rule of any foreign key constraint is that it must exist, as a primary key or unique constraint, in another table before inserted into the foreign key table.
This works great when you are adding a few rows at a time (traditional transaction processing environment). Howevere, you are trying to bulk insert into both at the same time. I'd term this as batch processing. Basically, the bulk update lock on the parent table is going to block the child table from reading it to check that the fk linkage is valid.
I'd say your 2 options would be 1.) leave the fk out entirely or 2.) Set the fk as nocheck before the bulk insert, then turn the check on after the bulk insert is complete with an alter table.

Resources