Showing posts with label database. Show all posts
Showing posts with label database. Show all posts

Sunday, 8 October 2017

Database Design and Performance


While designing the database below ideas helps to gain the better performance for the SELECT query


  • Compromise with Denormalization :If a significant number of your queries require joins of more than five or six tables, you should consider Denormalization.
  • Use Computed column :Instead of computing columns while reading better to add a computed column to calculate the value while DML. For example ORDER table have column Qty, Price, Discount. So suppose we want to total doller amount for each order then first we have to determine the dollar amount for each product.
         SELECT "Order ID", SUM("Unit Price" * Quantity * (1.0 - Discount))


        For a large set of orders, the query can take a long time to run. The alternative is to calculate the    dollar amount of the order at the time it is placed, and then store that amount in a column within the Orders table.


  • Decide Between Variable and Fixed-length Columns: Fixed length columns always take maximum space defined by the schema, even when the actual value is empty. The downside for variable length columns is that some operations are not as efficient as those on fixed length columns. For example, if a variable length column starts small and an UPDATE causes it to grow significantly, the record might have to be relocated. Additionally, frequent updates cause data pages to become more fragmented over time. Therefore, we should use fixed length columns when data lengths do not vary too much and when frequent updates are performed.


  • Use Smaller Key Lengths: An index is an ordered subset of the table on which it is created. It permits fast range lookup and sort order. Smaller index keys take less space and are more effective that larger keys. It is a particularly good practice to make the primary key compact because it is frequently referenced as a foreign key in other tables. If there is no natural compact primary key, you can use an identity column implemented as an integer instead
Reference :https://technet.microsoft.com/en-us/library/ms172432(v=sql.110).aspx

Friday, 29 September 2017

Meta Data of Greenplum Database



 When I learn any of the Databases, after knowing the basic architecture I always try to dig in Meta- Data of that database. Knowing meta- data always help me to handle that database smartly. Generally Meta-Data is for DBA but still it’s much helpful for the Database developer.
 What is Meta- Data?  Meta –Data is data about data. Every Database having a set of system table that stores the information about the data. For example when you create a table and specify the column and their data type, constraints etc. these all information store in to Database system tables that is called Meta data for that table. When we perform any of DML on that table database engine will check that Meta data if operation is not violating the constraints then it success else fail with the error.
How Meta- Data helps? 
  • Suppose I want to know how many of my table is having column data type as BIT?
select * from information_schema.columns where data_type='bit'
  • How many tables belong to a particular schema?
    select * from information_schema.tables where table_schema='schemaname'
  • What all Aggregate function Greenplum database provides?
            select * from pg_aggregate order by 1
There is so many useful information we can query from the Database Meta-Data, I will keep writing J