Here are the most often used command on Oracle and their equivalent in Redshift
1. Show Schema
Oracle Way:
1
2
3
4
| SELECTusernameFROMall_users; |
Redshift Way:
1
2
| SELECT *FROM pg_namespace; |
2. Describe a table and see the field names, types, encoding etc.
Oracle Way:
1
2
| |
Redshift Way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| SELECT DISTINCT n.nspname AS schemaname,c.relname AS tablename,a.attname AS COLUMN,a.attnum AS column_position,pg_catalog.format_type(a.atttypid, a.atttypmod) AS TYPE,pg_catalog.format_encoding(a.attencodingtype) AS encoding,a.attisdistkey AS distkey,a.attsortkeyord AS sortkey,a.attnotnull AS notnull,a.attencodingtype AS compression,con.conkey AS primary_key_column_ids,con.contype AS con_typeFROM pg_catalog.pg_namespace n,pg_catalog.pg_class c,pg_catalog.pg_attribute a,pg_constraint con,pg_catalog.pg_stats statsWHERE n.oid = c.relnamespaceAND c.oid = a.attrelidAND a.attnum > 0AND c.relname NOT LIKE '%pkey'AND lower(c.relname) = ''AND n.nspname = ''AND c.oid = con.conrelid(+)ORDER BY A.ATTNUM; |
3. Find Disk Usage Per Table
Oracle Way:
Oracle Way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
| SELECTowner,table_name,TRUNC(SUM(bytes)/1024/1024) MegFROM(SELECTsegment_name table_name,owner,bytesFROMdba_segmentsWHEREsegment_type = 'TABLE'UNION ALLSELECTi.table_name,i.owner,s.bytesFROMdba_indexes i,dba_segments sWHEREs.segment_name = i.index_nameAND s.owner = i.ownerAND s.segment_type = 'INDEX'UNION ALLSELECTl.table_name,l.owner,s.bytesFROMdba_lobs l,dba_segments sWHEREs.segment_name = l.segment_nameAND s.owner = l.ownerAND s.segment_type = 'LOBSEGMENT'UNION ALLSELECTl.table_name,l.owner,s.bytesFROMdba_lobs l,dba_segments sWHEREs.segment_name = l.index_nameAND s.owner = l.ownerAND s.segment_type = 'LOBINDEX')WHEREowner IN UPPER('BIC_DDL') -- PUT YOUR SCHEMANAME HEREGROUP BYtable_name,ownerORDER BYSUM(bytes) DESC |
Redshift Way :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| SELECT DISTINCT n.nspname AS schemaname ,c.relname AS tablename ,a.attname AS COLUMN ,a.attnum AS column_position ,pg_catalog.format_type(a.atttypid, a.atttypmod) AS TYPE ,pg_catalog.format_encoding(a.attencodingtype) AS encoding ,a.attisdistkey AS distkey ,a.attsortkeyord AS sortkey ,a.attnotnull AS notnull ,a.attencodingtype AS compression ,con.conkey AS primary_key_column_ids ,con.contype AS con_typeFROM pg_catalog.pg_namespace n ,pg_catalog.pg_class c ,pg_catalog.pg_attribute a ,pg_constraint con ,pg_catalog.pg_stats statsWHERE n.oid = c.relnamespace AND c.oid = a.attrelid AND a.attnum > 0 AND c.relname NOT LIKE '%pkey' AND lower(c.relname) = '' AND n.nspname = '' AND c.oid = con.conrelid(+)ORDER BY A.ATTNUM; |