Using view to pre-sort selection in mysql?

I have a table a with two fields, for example value1 and value2.

Each individual choice that I will make in this table is sorted by a value of 2 DESC, but since the table is quite large, I can no longer "vanilla" it because of a performance problem.

My solution was to create a v view, which was basically something like:

CREATE VIEW v AS SELECT * FROM a ORDER BY value2 DESC

Now my questions are:

  • Is it correct? Will this view really run once for my viewing, and not every time I receive it? And if not, how should I act instead?
  • How can I then select the data from it, making sure that the sorting remains the way I want, something like "SELECT * FROM v LIMIT 5"? Or do I need to cancel the order by clause when reading from the view (will it cancel the pre-sorting and repeat it)?

Thanks for any help!

+3
source share
3 answers

Will this view really run once for my presentation, and not every time I get it?

No, a non-materialized view (MySQL does not support materialized views) does not cache data - it is executed for each link.

, , , , - "SELECT * FROM v LIMIT 5"? order by ( )?

, ORDER BY .

ORDER BY . , ORDER BY , , ORDER BY . ORDER BY , , ( ORDER BY). :

CREATE VIEW your_view AS
  SELECT * FROM YOUR_TABLE ORDER BY value2;

  SELECT *
    FROM your_view
ORDER BY value1;

... ORDER BY - , .

ORDER BY - , . Eli MySQL ALTER TABLE ... ORDER BY :

ALTER TABLE a ORDER BY value2 DESC;

... INSERT/UPDATE/DELETE.

ORDER BY , . , .

+2

, " 2" DESC , ?

ALTER TABLE `table_name` ORDER BY `value2` DESC;
0

? , , ?

, ?

value2

How can I select data from it while making sure that the sorting is saved as I want, something like "SELECT * FROM v LIMIT 5"?

Yes. If the request has its own ORDER BY, it will be used ORDER BYfrom the view.

0
source

Source: https://habr.com/ru/post/1792398/


All Articles