Вы находитесь на странице: 1из 6

NETEZZA DATABASE USEFUL QUERIES AND TIPS

Query to get a list of views and their definitions in a database:


SELECT VIEWNAME, OWNER, CREATEDATE, DEFINITION FROM _V_VIEW WHERE
OBJTYPE='VIEW';
Query to get a list of tables in a database:
SELECT TABLENAME, OWNER, CREATEDATE FROM _V_TABLE WHERE OBJTYPE='TABLE';
Query to get a list of columns from a table or a view:
SELECT ATTNUM, ATTNAME FROM _V_RELATION_COLUMN WHERE NAME=UPPER
('<TABLE NAME>')
ORDER BY ATTNUM ASC;
Query to get list of user groups on the box:
SELECT GROUPNAME,OWNER,CREATEDATE,ROWLIMIT,SESSIONTIMEOUT,
QUERYTIMEOUT,DEF_PRIORITY,MAX_PRIORITY FROM _V_GROUP;
Query to get list of users and the groups they are in, on the box:
SELECT GROUPNAME,OWNER,USERNAME FROM _V_GROUPUSERS;
(Does not give any LDAP users in this query)
Query to find the number of rows in a table without actually querying the table:
(Sometimes needed for some really huge tables of row count > 80 Billion)
SELECT RELNAME TABLE_NAME,
CASE
WHEN RELTUPLES < 0
THEN ((2^32) * RELREFS) + ((2^32) + RELTUPLES )
ELSE ((2^32) * RELREFS) + ( RELTUPLES )
END NUM_ROWS
FROM
_T_CLASS,
_T_OBJECT
WHERE
_T_OBJECT.OBJID=_T_CLASS.OID AND
_T_OBJECT.OBJCLASS=4905 DISPLAY ONLY TABLES
AND RELNAME = UPPER('<TABLE NAME>');

Query to check if any of the SPU's are running slower than the rest:
(This actually gives the read-write speed of each SPU that is online)
SELECT HWID, BYTE_COUNT/TOTAL_MSEC
FROM
_VT_DISK_TIMING
ORDER BY 2;

One more query.. To get the list of tables and their skew and size:
SELECT TABLENAME,OBJTYPE,OWNER,CREATEDATE,USED_BYTES,SKEW
FROM _V_TABLE_ONLY_STORAGE_STAT
WHERE OBJCLASS = 4905 OR OBJCLASS = 4911
ORDER BY TABLENAME;

Use \dt in nzsql session to get the list tables


\dv to get list of views
\dmv - list of materialized views
\l - list of databases
\dg - list of groups
\du - list of users
\dpu - permissions set to a user
\dT - list of data types
\d <table name> - describes the table
\act - show current active sessions
\d - describe table(or view,sequence)
\dt , \dv , \ds , \de - list tables,views,sequences,temp tables
\dSt , \dSv - list system tables and views
\df - list functions
\l - list databases
\dT - list data types
\du - list users
\dg - list groups
\dpu - list permissions granted to a user
\dpg - list permissions granted to a group

Steps to restore table in Netezza database:


1) Find the backup history of database you want to restore by running below query.
select DBName, DBNAMEORIG, OPTYPE as Backup_Type, StartTime, BackupSet, Logfile
From _v_backup_history where DBName = 'TEST_DB'
-- DBNAMEORIG = 'TEST_DB'
and starttime > '2013-08-01 05:13:13'
Output of above sql will be:
DBNAME DBNAMEORIG BACKUP_TYPE STARTTIME BACKUPSET LOGFILE
TEST_DB TEST_DB FULL 2013-08-19 07:40:18 20130819114018 backupsvr.1649.2013-08-19.log
TEST_DB TEST_DB FULL 2013-08-12 06:39:15 20130812103915 backupsvr.29095.2013-08-12.log
TEST_DB TEST_DB FULL 2013-08-05 07:09:39 20130805110939 backupsvr.9521.2013-08-05.log
Here we pull information from column DBNAMEORIG also; this will be helpful if the database need to
be restored is deleted.
2) From above we can find the backup set that needs to be restored
3) Now you can find the path where backup was taken by looking into the log file by going to path
/nz/kit/log/backupsvr
4) Now run below script to restore the table.
/nz/kit/bin/nzrestore -dir /netezza/backup_Path/testserver.domain.com -backupset 20130812103915
-tables Table_Name -db TEST_DB
NOTE: If table already exists then this command will overwrite that table.
In case you have differential backups and want to restore form there as well then command will be like

/nz/kit/bin/nzrestore -dir /netezza/backup_Path/testserver.domain.com -backupset 20130812103915


-increment 3 -tables Table_Name -db TEST_DB
Or use below command if you have to restore this table in different database
nzrestore -db TEST_DB1 -sourcedb TEST_DB -backupset 20130812103915 -tables Table_Name -dir
/netezza/backup_Path/testserver.domain.com

Crontab job scheduling Tips

How to schedule a cron job to run every 1st and 3rd Friday of the month

15 20 1-7, 15-21
* * bash -c . ~/.bashrc ; [ "`date +\%a`" = "Fri" ] && /my_server/scripts/backup.bsh >>
/my_server/log/backup.log_`date +\%Y\%m\%d` 2>&1

How to schedule a cron job to run every 2nd and 4th Friday of the month

05 20 8-14, 22-28
* * bash -c . ~/.bashrc ; [ "`date +\%a`" = "Fri" ] && /my_server/scripts/backup.bsh >>
/my_server/scripts/backup.log_`date +\%Y\%m\%d` 2>&1

How to schedule a cron job to run every first Saturday of each month at 2:10 PM

10 14 1-7
* * bash -c . ~/.bashrc ; [ "`date +\%a`" = "Sat" ] && /my_server/scripts/backup.sh >>
/my_server/scripts/backup.log_`date +\%Y\%m\%d` 2>&1
To check Databases without any backup:

Select datname DatabaseName from _T_database where datname not in (select dbname from
_v_backup_history) and datname not like 'MASTER_DB'

Useful Tips:
The great thing about Netezza is it does soft deletes until the groom process runs. This means your
deleted data is really still there. This is of course very important if you just deleted something you didnt
mean too. And since updates are really just a delete of the old values and an insert of the new behind
the scenes, you can actually undo updates too.
So first a little about how Netezza handles transactions. Every insert, update, or delete transaction is
assigned a sequential transaction id or xid. You can see this id by querying the column create xid . Also
there is a column to indicate deleted rows called delete xid. This is set to 0 if this is a readable, not
deleted row. When the row gets deleted, this column gets populated with the transaction id assigned to
the delete or update statement. And as I mentioned above, updates are handled by doing a delete of the
old record and inserting the entire new row with the new values.
Under normal circumstances when you run a select you will not see rows that have a delete xid not equal
to zero. But there is a simple session variable that can be set that allows you to see these deleted rows.
The simple command to run in your favorite query tool is
set show_deleted_records = true
So to see whats there you can then just select from your table and include the 2 transaction id
columns, like ...
Select createxid, delete xid, * from your_table
If you want to see just the deleted rows, just select where deletexid is not zero
Select createxid, deletexid, * from your_table where deletexid! =0
Once you can see your deleted data, and figure out which transaction you are trying to undo, you
can simply re-insert the data

insert into your_table select * from your_table where deletexid=233443; transaction id from delete.

To undo an update, just re-insert the deleted rows and delete the inserted rows.
insert into your_table select * from your_table where deletexid=233443;transaction id from update
delete from your_table where createxid=233443; transaction id from update
And Im stating the obvious, but to undo an insert, you dont even need to show deleted rows, just
delete by the transaction id
delete from your_table where createxid=233443; transaction id from insert