Showing posts with label query. Show all posts
Showing posts with label query. Show all posts

Friday, July 24, 2015

Postgres Query: Given an index find associated table

select
    t.relname as table_name,
    i.relname as index_name,
    array_to_string(array_agg(a.attname), ', ') as column_names
from
    pg_class t,
    pg_class i,
    pg_index ix,
    pg_attribute a
where
    t.oid = ix.indrelid
    and i.oid = ix.indexrelid
    and a.attrelid = t.oid
    and a.attnum = ANY(ix.indkey)
    and t.relkind = 'r'
    and i.relname = 'Index_to_search_for'
group by
    t.relname,
    i.relname
order by
    t.relname,
    i.relname;
 
 
Source:
http://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql

Tuesday, September 22, 2009

Oracle Space queries

Get Used space for current user
SELECT sum(bytes)/(1024 * 1024) as "Used (MB)"
FROM user_SEGMENTS
ORDER BY 1 desc

Get tablespace usage for all users using that tablespace
select owner, sum(bytes)/power(2,20)mb
from dba_extents
where tablespace_name = 'TABLESPACE'
group by owner
order by 2 desc;

Get Total tablespace available for current user
select tablespace_name,round(sum(bytes) / (1024 * 1024),2) "Tablespace SIZE (MB)"
from user_free_space
group by tablespace_name

Get space of individual tables/indexes (objects) in a schema
select sum(bytes) / (1024 * 1024) as MB,owner,segment_type, segment_name
from dba_segments s
where owner = 'SCHEMA'
group by owner,segment_type, segment_name
order by MB desc

Get USER_ dictornary Tables
SELECT table_name, comments
FROM dictionary
WHERE table_name LIKE 'USER_%'
ORDER BY table_name;

http://snipplr.com/view/4748/get-a-list-of-all-the-user-tables-oracle/
http://www.freelists.org/post/oracle-l/dba-extents-vs-dba-segments,8

Sunday, February 01, 2009

Djanog show model query

Show the raw SQL generated by a model access: 
# DEBUG needs to be set True 
>>> from django.db import connection 
>>> connection.queries [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls', 'time': '0.002'}]

sql # The raw SQL statement 
time # How long the statement took to execute, in seconds.

Reset the queries list returned above:
>>> from django import db
>>> db.reset_queries()

Show SQL for a given query (newer django version; 1.5+?)
>>> Product.objects.all().query.sql_with_params()

Show SQL for a given query (older django version; 1.2-1.5+?)
>>> Product.objects.all().query.as_sql()