How can we make 'create table as select' support order?

Source: Internet
Author: User

How can we make 'create table as select' support order?

As we all know, the SQL command "CREATE TABLE as select" does not support "order by", but if we want to add a table, it stores a group of data that has been arranged, what should I do if the data comes from an existing table?

Haha, it's a good idea to create a view first and then create again. Let's take a look at the following article.ArticleAnd may be useful to you.

Generally, sorting data in a table is helpful to optimize the query performance of the database.

For example, your applicationProgramA Query Needs to be executed frequently.
Select * from EMP where name like'm % ';

If all rows in the EMP table are sorted by their names, your query will be significantly improved. Because all the rows whose names contain 'M' are stored together, Oracle only obtains a small amount of data blocks from the table when querying data. Of course, if the data in your table is static, this is very good, that is, you can insert in order when you create a table. However, data in our table is often updated, and SQL does not support insert into with the order by clause... select... from... and create table as select... from... command, so we can only discard the inherent sorting. When the data reaches tens of thousands of records, the query performance is significantly reduced. There are two solutions to this problem.

Here we provide a method to do this through the group by operation.

Create view empgroup
Select name, empid, hiredate, rownum from EMP
Group by name, empid, hiredate, rownum;

Use this view to add a table

Create Table sorted_emp as select name, empid, hiredate from empgroup;

Note:

1. If you use oracle7.3 or later, you can use inline view instead of empgroup view.

2. If you ignore rownum when creating a view, a large number of repeated records will appear due to the use of group.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.