- What is a Query Plan?
- A query plan is a set of steps that the database management system executes in order to complete the query.
- The reason we have query plans is that the SQL you write may declare your intentions, but it does not tell SQL the exact logic flow to use. The query optimizer determines that. The result of that is the query plan
- The lesson to learn from this when in doubt check the execution plan. If you feel a query is running slow and an equivalent query, such as a join, may be faster, write one up and check the plan. Check to see which uses more efficient steps. This much better than guessing. As you get better a reading plans you’ll start to notice things about your databases, such as whether you need to add an index.
- Database Indexes
- Indexes are special lookup tables that the database search engine can use to speed up data retrieval.
- An index helps to speed up SELECT queries and WHERE clauses, but it slows down data input, with the UPDATE and the INSERT statements. Indexes can be created or dropped with no effect on the data.
- Single-Column Indexes
- A single-column index is created based on only one table column.
- Unique Indexes
- Unique indexes are used not only for performance, but also for data integrity. A unique index does not allow any duplicate values to be inserted into the table. The basic syntax is as follows
- Composite Indexes
- A composite index is an index on two or more columns of a table. Its basic syntax is as follows.
- CREATE INDEX id_index ON students (stdId) USING BTREE;
- Will do ab binary search on stdId column and it increases the performance.
- A database index is somewhat similar to this table of contents in a book. Indexing will help a data base query to be retrieved fast (Because the query does not require to go through the entire table to get the data, but it will find the data blocks from the index.).
- What is the difference between UNION and UNION ALL?
- UNION removes duplicate records (where all columns in the results are the same), UNION ALL does not.
- There is a performance hit when using UNION instead of UNION ALL, since the database server must do additional work to remove the duplicate rows, but usually you do not want the duplicates.
- How to retrieve data from json string saved in a column?
- JSON
-
{"male" : 2000, "female" : 3000, "other" : 600}
- Query
-
SELECT ID, CITY, json_extract(POPULATION_JSON_DATA, '$.male') AS POPULATION_MALE, json_extract(POPULATION_JSON_DATA, '$.female') AS POPULATION_FEMALE, json_extract(POPULATION_JSON_DATA, '$.other') AS POPULATION_OTHER FROM JSON_TABLE;
- Oracle bind variables?
- Using :variable instead of direct values
- Before Oracle runs a SQL statement it checks it's valid and determines how to access the tables and join them together. This is called parsing. The optimizer has the task of figuring out which table access and join methods to use. This produces an execution plan. When Oracle sends a statement to the optimiser to do this it's called a hard parse.
- If a plan already exists for a query, Oracle doesn't need to go through the optimization process again. It can reuse the existing plan. This is referred to as soft parsing.
-
select * from orders where order_id = :ord;
- If we run this query twice in succession with different values for :ord there's one hard parse and one soft parse and increases the performance.
Friday, 12 October 2018
SQL Interview Questions
About Unknown
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment