Running Sql Queries On Production/Live Databases - Five Things To Remember

Did you lot ever human face upwards a province of affairs where some of your innocuous looking activity has movement production number together with that also big time? Well, I promise you lot receive got non because it's certainly, non a pleasant experience. One of such innocuous looking activity is running SQL queries on production databases. I had that inwards past times real early on inwards my career where I removed some config every bit duplicates entirely to uncovering afterward a calendar week that it stopped publishing messages to ane of the downstream. When you lot move inwards complex systems which has so many components, millions of lines of code, thousands of configuration together with many databases alongside hundreds of tables, you lot receive got to hold upwards actually careful alongside anything you lot do. Often in that location is on the existent agency to perform a production-like testing, thus the best bet is to buy the farm along your modify every bit much isolated together with express every bit possible.

Anyway, all this background because today I am going to part some tips to you lot guys spell querying production or alive databases to preclude production issues. Since many Java developers are non SQL experts but they create write SQL queries, stored procedures, together with interact alongside both examination together with production databases, in that location is a goodness adventure that their innocuous looking activity tin movement production issue.

Last twelvemonth at the same time, nosotros had ane of such incident where a developer's SELECT inquiry blocked some physical care for inwards production. The innocuous looking SELECT inquiry holds a lock on ane of the tables which were needed past times the physical care for trying to update together with insert the information into the same table. The developer run the inquiry at the terminate of the twenty-four lx minutes catamenia together with forgot virtually that, entirely to uncovering adjacent morn that some essential chore hasn't completed together with they are running from terminal night. Eventually, DBAs were involved together with they kill the connective which is blocking the chore together with things were restored back.




5 Things to regard spell running SQL queries on production databases

Well, this is ane of the extreme cases where developer forgets to cancel the inquiry when it was taking a long fourth dimension but probability of happening something similar this is quite high, particularly if you lot receive got production access together with don't know much virtually locking inwards the database. The best is to improve your noesis of locking together with isolation degree but to hold upwards condom than sorry, you lot tin also follow next tips spell running SQL queries on production:


1) Always inquiry alongside NOLOCK option
This tin movement production number if some chore is also running together with trying to update the same tabular array you lot are querying. By proverb NOLOCK you lot trim back the peril of blocking together with deadlock e.g.

SELECT Id, Name, Address from Employee alongside (NOLOCK) where Id= 2

When you lot run your inquiry alongside NOLOCK hint, it instructs inquiry engine to non number shared locks together with does non honor exclusive locks. When you lot purpose alongside NOLOCK option, it is possible to read an uncommitted transaction or a laid upwards of pages that are rolled dorsum inwards the middle of a read. Dirty reads are possible. Though, worth noting is that this selection entirely applies to the SELECT contention together with available for Microsoft SQL Server. See Microsoft SQL Server T-SQL Fundamentals larn to a greater extent than virtually tabular array together with row degree locking.



2) Always run your inquiry inwards backup or secondary server
If possible, e'er run your inquiry on backup or secondary server instead of running it on the primary server. Only if merely cannot purpose secondary because you lot experience information is non most up-to-date purpose primary, but bottom business is to avoid touching the primary or principal server on production hours.


3) Test your queries on UAT earlier running on production
This is the same dominion which I mightiness receive got told you lot earlier when explaining virtually UNIX commands. Similar to UNIX command which you lot should examination on Staging boxes earlier running on production machines, you lot should also showtime run your inquiry on QA or UAT environs earlier running them inwards production. This non entirely ambit you lot a goodness thought of what to facial expression but also salve you lot from syntax error together with accidental mistakes inwards production.


4) Avoid touching Production database during Market hours
If you lot are working for a organization which has some marketplace hours e.g. stock exchanges which run from morn ix to 4 PM, together with so you lot should avoid touching your production database during that catamenia together with entirely run your queries afterward marketplace hours. There is a lot of activity goes on DB during marketplace hours together with in that location is e'er a goodness adventure that your innocuous looking SELECT queries may interfere alongside them.


5) Have a 4 optic banking concern fit on your query
If you lot are non working lonely together with receive got some squad members, you lot tin e'er enquire your colleagues to create a 4 optic banking concern fit on the inquiry you lot desire to run inwards production. If you lot tin review your queries from DBAs together with so it's fifty-fifty better.

 Did you lot ever human face upwards a province of affairs where some of your innocuous looking activity has movement produ Running SQL Queries on Production/live Databases - five Things to Remember


That's all virtually some of import tips to retrieve spell querying production database. You should also larn to a greater extent than virtually how database executes your SQL inquiry e.g. how the index works, how locking works, tabular array scan, row scan, tabular array degree locking, or row degree locking etc. If you lot know those basics well, you lot tin predict your queries conduct to a greater extent than deterministically together with potentially avoid unpleasant surprises. One mass which tin assistance you lot hither is the SQL Performance Explained past times Markus Winand, this volition ambit you lot a goodness thought of the database from developer's perspective.

What virtually you? What tips you lot follow or what caution you lot receive got spell querying production/live database?

Further Learning
list)
  • The Developer's listing of must read SQL books (list)
  • Difference betwixt where together with having clause inwards SQL (answer)
  • The departure betwixt matrimony together with matrimony all inwards SQL? (answer)
  • Difference betwixt row_number, rank, together with dense_rank inwards SQL (answer)
  • The departure betwixt self together with equijoin inwards SQL? (answer)
  • Thank you lot for reading this article, if you lot like, delight part alongside your friends together with colleagues too. It helps a lot.

    0 Response to "Running Sql Queries On Production/Live Databases - Five Things To Remember"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel