thedatabasezealot.com Rotating Header Image

RMAN Scripting on Windows

Just been writing a basic script that can run from a Windows batch file…

Just thought I’d paste it here… I’m going to build this up to include a form of logging/monitoring (email and network dependant though).

rman target / nocatalog

run {
allocate channel dis1 type disk;
backup
 format ‘D:\backups\%d_t%t_s%s_p%p’
 (database); 
release channel dis1;
}

run {
allocate channel dis1 type disk;
backup
format ‘D:\backups\log_%t_%sp%p’
 (archivelog all delete input);
release channel dis1;
}

This first component connects to the target DB (ORCL) and uses the nocatalog switch.  This is to store RMAN information internally and not externally on an RMAN catalog DB.

The second component backs up the database.

The third component backs up the archive log files, and as it does – deletes the archivelog files (as they are now stored in the backup piece).

Cheers

Explicit conversion

So… I haven’t posted for a while.  Sorry about that folks.

Explicit conversion… always do it! Never EXPECT anything consistent aka Implicit Conversion.

Example… I recently built a stored procedure where different queries needed to be fired dependant on a provided key-phrase.

As we can always typo, I used the classic UPPER(<string>) conversion. i.e.

If UPPER(x_keyphrase) = ‘OPTIONONE’ then

….

elsif UPPER (x_keyphrase)=’OPTIONTWO’ then

….

etc.

One thing I did that was stupid.  When I built this, the developer had kindly and professionally supplied the key phrases they’d be using.  These were in mixed case.  At the time my brain was celebrating the ease at which building the stored procedure would be… and ended up using them in the IF clauses directly…. guess what.. a week later when the developer started to test the procedure, it didn’t work.

We spent ages looking at the code… ‘WHY WON’T IT WORK?’… then it clicked.  I was applying the UPPER clause against mixed case key-phrases.  Dumb. Dumb. Dumb!

So… the moral of the story:

1) use explicit conversion.

2) make sure the key-phrases match the explicit (in this case UPPER).

3) with date types, always use to_date conversion… otherwise SQL injection is possible (refer to Tom Kytes blog for more on this… he gives an elegant example)

Cheers

Effective Technical Partnerships Part 1: DBAs and Developers Unite!

Ok so… here’s my philosophy on the whole us vs them, jedi vs sith or goodies vs badies…

I don’t think I’ve ever worked anywhere (uh oh… pot – kettle – black…) where developers and DBAs get on.  Now I don’t mean it’s a gun battle in the working environment.  What I mean is each groups views and thoughts on the database can be so different that it leads to conflict.  Conflict that escalates to a long term blood feud (sometimes… or maybe I’m exaggerating to make my point. Got it? Good!).  Conflicts usually reside from these core areas (for either side – DBA or Developer):

  • Lack of knowledge
  • No prior agreement on technical processes
  • Lack of interaction
  • Poor understanding of other individuals knowledge and experience
  • Time and cost pressures
  • The “That’s not best practice…” Syndrome which gridlocks progessing through a technical issue.  (yuk!)

So… I’ve been working on a presentation called ‘Design Charters’, which is basically a charter/constitution/contract that is ‘signed’ and agreed on by all working on a development project.  This charter agrees the technical and the social interactions between the developer->application code->database->DBA->back round to developer.  This is part of a set of processes that I have created/developed/designed (Probably not… who has an original idea these days.. but anywhoooo), which will encourage working together, reduce poor communication and understanding in a project and allow the creative juices of both DBA and Developer to flow into one cohesive force. 

As the presentation is in DRAFT still…. I’ll leave you with ‘Watch this space’.

I’ll post up this first presentation soon…… real soon.  Promise :)

I need to come up with a super cool name for the entire package of processes/work ethics/practices … because to be honest, it’s a super cool framework (theoretical still… but will be in force as soon as I can test it…) which encourages planning, efficiency and cohesiveness.

If someone comes up with a cool name for this framework (comment please) then I’ll buy them coffee and maybe lunch…. (Wellington based only).

Oracle: Generic vs Traditional Data Models

So recently, the topic of the moment in my brain has been generic table design vs traditional.

Back when I was a developer (sorry to come out of the closet like that….haha), I worked with a team of very skilled developers on a major project.  The table designs used a mix of generic and traditional data model methods.

I can hear some of you thinking, ‘Uh?’.

Let me elaborate:

Traditional Data Models

This data model uses the standard primary unique numbered key, with data normalized to roughly the 3NF (3rd Normal Form).  This means the database is able to use the standard tools of attack when parsing a query (index range scans, clustered index scans, etc).  This makes for a faster, efficient data model (if designed correctly).  It basically uses foreign key ids to maintain integrity.  This is the safest model for data integrity and speed.

Generic Data Models (aka Nested Table Method)

So a “new” method of data modeling proving to be popular at the moment, and let me just say I use the word “new” very loosely, is the storing of primary keys as non-defined columns.  Non-defined in that they are neither stored as PK or FK columns but as varchar2.  The other data within the table is also generic columns.  Now this causes an issue in terms of performance.  No longer do the traditional PK system generated indexes get to come into force – the optimizer relies on the competency of the developer and DBAs involved.

Moving on…. so often with bespoke systems which have been developed over a series of years (usually 5 plus) development styles of people involved in the system get mixed up – new breed developers come on board to projects, keen to prove something.  The old school developers bounce up too, often cynical (not all but some) of any new practice.  I’ve met developers who insisted on:

  • NOT changing the data model, requiring any addition (even if changing current primary data structures) to have a whole new set of tables and relationships to be created.  Messy and performs like a bag of kittens!
  • Implementing scripts found on other people’s blogs with no prior testing. Errr…which leads to arguments with DBAs…normally me :)
  • Keen to redevelop an entire data model in their next modularly related project.  (Yep.  It’s true.)
  • Insist on doing things ‘Their way’, which is hard basket – because often they don’t want to investigate any other option presented, resulting in heads being banged into brick walls.  Painful.

Seeing as I was a developer for some time…. it pains me to see this pattern of behavior through the professional developer market.  It pains me more when I am forced along the path of pointing out some of the flaws in the solutions presented.  What’s even more scary is when my clients are having to pay out for – dare I say it – school boy quality work.

Hold on a second – let me stop the rant before I look like a sour grape.  I am not trying to say here that I am perfect or that I know all – my point is this: Developers embrace what colleagues and business associates are saying to you.  Test the options – often what you believe to do the right path, can be the wrong one.  Testing options on solutions is the best way to prove you are right…. no need for textual arguments.  It’s petty and when you’re wrong, can do some harm to either the relationship to your client or to your own credibility as an expert in the field.

So… let’s get back on point.  It’s ok to use generic tables.  They are useful in so many different contexts.  One major negative: on general they are s l o w.  They can’t integrate into an existing data model easily, particularly if a system has been in full swing for 15 years, hard coded and developed on a very traditional data model.

Stay away from primary keys being stored as references in child tables.  It’s clunky, it’s slow and it won’t work.

So when looking at how to implement a data model, either for a project or for an existing system, think about the purpose of your code.  Think about whether it needs to be fast.  Think about what it is doing.  Does it need to work that way? Can you do with out child to parent built queries?  Is there a ‘trick’ way of presenting the information to the user?  Sometimes code doesn’t need to appear sophisticated, it just needs to work.

Remember that.

Signing off for now.

TDZ.

SQL Server: Full backup, untamed transaction log and a mirror

I have read so many blogs, forums and frequently asked questions on this topic – it’s insane. 

What is even more insane is I’m going to blog about it.  If you have any questions, comment below and I will get back to you.

So… it usually starts like this:

‘Dear XYZ,

I have a huge transaction log and it won’t go any smaller.  I’ve backed up the database every night since the first world war.  I regularly perform full backups and the thing won’t shrink back down to size. Help?

Regards DBA Jr.’

So, without repeating thousands of others and to shake things up a bit – a list:

  1. Running in FULL recovery mode means you need to put a maintenance plan together which backs up the transaction log regularly – the SQL Server fairy (yes – she is real) will not do it for you.  If the database is bigger than 1 Gb, and is used for high write activity, like a help desk system for example: Back that log up on at least a 30 minute basis. Otherwise twice a day is probably wise (lunch time and then after your main full backup).
  2. Something to think about: When you backup your transaction log, IF the transaction log hasn’t grown from it’s original created size (Check the LDF properties right now), then the transaction log won’t shrink but at the same time it will.  What do I mean by that?  Let me give a visual example.  You have a bottle and it’s full of water.  The bottle is your OS LDF file.  The water is your database transactions.  When you back up, the water is poured out of the bottle into another one.  You still have your bottle.  Physically it still takes up the same space but inside it is empty…  Are you with me on this? I hope so.
  3. If you run a mirror from the database, there is a chance some of the transactions haven’t mirrored to the destination server aka partner.  If this is the case, you can’t have the file shrunk down to size.
  4. NEVER TRUNCATE_ONLY.  Why? It threatens your system.  It threatens your ability to restore to a point in time.  It threatens your livelyhood.  Don’t do it.  I did read somewhere that Microsoft have deprecated this option in SQL Server 2008.  Good because it sucks and too many people play with fire and burn there entire house down!
  5. A wise man once asked me (and told me :) ): When is your backup considered to have been successful?  The answer: When you have restored from it.  Never underestimate the importance of regularly backing up but also test those back ups occasionally too!  I have seen backup tests fall on their butts – BADLY.  All because the process had never ever been tested in the 3 years of the applications life.  The tape with the archived backup was damaged.  This from a professional tape storage company.  Crap happens – plan for it.  Daily!
  6. If you encrypt your database: BACKUP YOUR CERTIFICATES.  Regularly. Go on.  Go and do it now.

Look – no one is perfect and we certainly don’t need to be anal about backups but to be quite honest, the best DBAs in the world are the one’s who can do this one basic important task.

I cannot overstate enough the importance of practicing a restore of an application.  You’ll be amazed sometimes at how many other components are related and required for even the simplest of applications.  Don’t get caught out.  Do some restore testing of all your core systems.

If you have any questions on backups then feel free to ask and I’ll answer them poste haste.

Peace.

The Database Zealot