MDF file is keeping deleted data - sql-server

How to get rid of deleted data (names, addresses etc) which no longer exist at database but .MDF file still has them.
In my country there is a law obligating me to get rid of these data but I can't.
I've switched database (SQL SERVER 2005) to Simple recovery model, performed full backup and shrinked database and files (data file and log file).
The data still pesist in data file (MDF).

Is the table a heap?
Heaps do not physically remove the data when you delete rows. To reclaim the space create a clustered index on the table.

Related

Backup specific tables from production into a .bak file (not BCP) in SQL Server

I need to backup a couple of tables from my prod database.
Each table has 250 million rows.
The complete backup is almost 1TB
I don't want the full backup due to space (Needs double space for backup & restore)
I can't use BCP due to volume and heavy inserts involved.
SQL Server backups (.bak) are always all or nothing.
The only "selection" you can get is if you had multiple filegroups - then you could back up just one filegroup and ignore all others.
But you cannot selectively backup just a handful of tables into a .bak file. There's just no feature for that.
Create an auxiliary database, select from your 250mil tables into corresponding columnstores (in mil batches) , backup the aux db and you have your data "suitcase"

SQL recovery - partitioned table (file groups) - each on separate disk (and one of disk crashes)

For huge tables I am thinking of using the concept of partitioning using file groups and have each file group on a separate disk. My question is that -
If one of the file group disk crashes, then should this incident be treated as database crash? Will it cause the database to stop working?
Will the restore operation (assuming full backup was taken) automatically create the file groups as configured before the crash?
If one of the file group disk crashes, then should this incident be
treated as database crash? Will it cause the database to stop working?
If all your data from that disk is in memory, you'll not even notice this crash.
Until a checkpoint attempts to write on that disk, or you need to read from it a new portion of data(if it's not in memory) you'll be able to work without any error.
Will the restore operation (assuming full backup was taken)
automatically create the file groups as configured before the crash?
Your question is not clear.
You can restore certain filegroups from full backup, but if these filegroups are not readonly you'll not be able to reconcile them with the rest of your database. It will be
only possible if your db is in full recovery model and you take and restore tail of the log backup after your full backup (and may be other log backups in between).
Here you can read more in details Piecemeal Restores (SQL Server)

Does Shrinking A Log (.ldf) Database File Delete Temp Tables

I was just shrinking some .ldf log files to reduce some space on my server and I was wondering if when I do so if I am deleting any temp tables that are currently on that server? I wouldn't think so because I would think that those would be stored in the .mdf Data files, but just figured that I would ask!
Thanks!
Temp tables are created in the tempdb database only. Shrinking files on user databases will not impact tempdb.
Temp tables are objects, and are stored in the data file. Log files contain the transaction history of the database. Maintenance operations on a log file could remove the history in the file, but will not remove objects.
Shrinking a database file simply removes unused pages to free up space in a file. If a page is being used, it will not be touched by the shrink operation.
So the answer to you question is no, shrinking log files will not delete temp tables.

SQL Azure Export/Bacpacs and Foreign Key Integrity

I've a bit of a strange problem with a BACPAC I took last night using the SQL Azure Import/Export Service.
In our database there are 2 related tables.
dbo.Documents --All Documents in the database
Id
DocName
Extension
dbo.ProcessDocuments --Doc's specific to a process
Id
DocumentId (FK -> dbo.Documents.Id with Check Constraint)
ProcessId
Based on that Schema it should not be possible for the ProcessDocuments table to include a row that does not have a companion entry in the main Documents table.
However after I did the restore of the database in another environment I ended up with
7001 entries in ProcessDocuments. Only 7000 equivalent entries for them in Documents (missing 1). And the restore failed on attempting to restore the ALTER TABLE CHECK CONSTRAINT on ProcessDocuments
The only thing I can imagine is that when the backup was being taken, it was sequentially (alphabetically???) going through the tables, and backing up the data 1 table at a time and something like the following happened.
Documents gets backed up. Contains 7000 entries
Someone adds a new process document to the system / Insert to Documents & Process Documents
ProcessDocuements gets backed up. Contains 7001 entries
If that's the case, then it creates a massive problem in terms using BACPACs as a valid disaster recovery asset, because if they're taken while the system has data in motion, it's possible that your BACPAC contains data integrity issues.
Is this the case, or can anyone shed any light on what else could have caused this ?
Data export uses bulk operations on the DB and is NOT guaranteed to be transactional, so issue like you described can and eventually will happen.
"An export operation performs an individual bulk copy of the data from each table in the database so does not guarantee the transactional consistency of the data. You can use the Windows Azure SQL Database copy database feature to make a consistent copy of a database, and perform the export from the copy."
http://msdn.microsoft.com/en-us/library/windowsazure/hh335292.aspx
if you want to create transactionally consistent backups you have to copy the DB first (which may cost you a lot, depending on size of your db) and then export a copied DB as BACPAC (as ramiramilu pointed out) http://msdn.microsoft.com/en-us/library/windowsazure/jj650016.aspx
you can do it yourself or use RedGate SQL Azure Backup but from what I understand they follow exactly the same steps as described above, so if you choose their consistent backup option it's gonna cost you as well.
As per the answer from Slav, the bacpac is non-transactional and will be corrupted if any new rows are added to any table while the bacpac is being generated.
To avoid this:
1) Copy the target database, which will return straight away, but the database will take some time to copy. This operation will create a full transactional copy:
CREATE DATABASE <name> AS COPY OF <original_name>
2) Find the status of your copy operation:
SELECT * FROM sys.dm_database_copies
3) Generate a bacpac file on the copied database, which isn't being used by anyone.
4) Delete the copied database, and you'll have a working bacpac file.

Empty database over 10GB in size

While I was trying to populate a table from a 11GB text file (.tsv) I got the error:
Could not allocate space for object 'X' in database 'Y' because the 'PRIMARY'
filegroup is full. Create disk space by deleting unneeded files, dropping objects
in the filegroup, adding additional files to the filegroup, or setting autogrowth
on for existing files in the filegroup.
I thought the problem was the database exceeded 10GB, which is the limit for SQL Express. Yet, when I checked, I noticed the database is actually empty. The query
SELECT TOP 10 *
FROM Table
Takes several minutes to execute and it gives me back nothing. When I checked under properties > files I found out filegroup PRIMARY is actually 10240MB.
What is this about, and how can solve the problem?
Populating 11GB text file into SQL server express will give an error since it will exceed the edition limit. You either split the text file into 2 files or upgrade to standard edition. Or try Oracle XE? It store up to 11GB :) might be just enough for you.

Resources