基于对的第二个元素,如何对对的向量进行排序?(How do I sort a vector of pairs based on the second element of the pair?)
如果我有一对矢量:
std::vector<std::pair<int, int> > vec;
是否有简单的方法来根据对的第二个元素以增加的顺序对列表进行排序?
我知道我可以编写一些功能对象来做这些工作,但是有没有办法使用STL和
std::less
现有部分来直接进行工作?编辑:我明白我可以写一个单独的函数或类来传递给第三个参数进行排序。 问题是我是否可以用标准的东西来构建它。 我真的会看起来像:
std::sort(vec.begin(), vec.end(), std::something_magic<int, int, std::less>());
If I have a vector of pairs:
std::vector<std::pair<int, int> > vec;
Is there and easy way to sort the list in increasing order based on the second element of the pair?
I know I can write a little function object that will do the work, but is there a way to use existing parts of the STL and
std::less
to do the work directly?EDIT: I understand that I can write a separate function or class to pass to the third argument to sort. The question is whether or not I can build it out of standard stuff. I'd really something that looks like:
std::sort(vec.begin(), vec.end(), std::something_magic<int, int, std::less>());
原文:https://stackoverflow.com/questions/279854
最满意答案
如前所述,“使用filesort”和“使用临时”并不总是意味着糟糕的性能。
以下是提高ORDER BY语句性能的一些基本指导原则。 亮点:
如果你想增加ORDER BY的速度,检查你是否可以让MySQL使用索引而不是额外的排序阶段。 如果这不可行,您可以尝试以下策略:
增加sort_buffer_size变量的大小。
增加read_rnd_buffer_size变量的大小。
通过声明列的大小,每列使用更少的RAM,因为它们需要保存存储在其中的值。 例如,如果值不超过16个字符,则CHAR(16)优于CHAR(200)。
首先尝试使用索引(确保您按排序的字段有索引)。 请注意,增加系统变量sort_buffer_size和read_rnd_buffer_size也会对其他查询产生负面影响 - 请考虑将它们专门设置为您需要的会话,并将其保留为所有其他会话的默认设置。
As said already, "using filesort" and "using temporary" do not always imply bad performance.
Here are some basic guidelines for improving performance of ORDER BY statements. The highlights:
If you want to increase ORDER BY speed, check whether you can get MySQL to use indexes rather than an extra sorting phase. If this is not possible, you can try the following strategies:
Increase the size of the sort_buffer_size variable.
Increase the size of the read_rnd_buffer_size variable.
Use less RAM per row by declaring columns only as large as they need to be to hold the values stored in them. For example, CHAR(16) is better than CHAR(200) if values never exceed 16 characters.
First try to use indices (make sure the fields you are sorting by have indices). Note that increasing the system variables sort_buffer_size and read_rnd_buffer_size can also have a negative effect on other queries - consider setting them specifically for the session you need them for and leave them at default for all other sessions.
相关问答
更多-
如前所述,“使用filesort”和“使用临时”并不总是意味着糟糕的性能。 以下是提高ORDER BY语句性能的一些基本指导原则。 亮点: 如果你想增加ORDER BY的速度,检查你是否可以让MySQL使用索引而不是额外的排序阶段。 如果这不可行,您可以尝试以下策略: 增加sort_buffer_size变量的大小。 增加read_rnd_buffer_size变量的大小。 通过声明列的大小,每列使用更少的RAM,因为它们需要保存存储在其中的值。 例如,如果值不超过16个字符,则CHAR(16)优于CHAR ...
-
为什么MySQL在没有ORDER BY的情况下使用filesort?(Why is MySQL using filesort when there is no ORDER BY?)[2022-03-29]
无论何时执行GROUP BY MySQL都会默认进行排序。 为了避免这种情况,只需添加ORDER BY NULL 。 MySQL will sort by default whenever you do a GROUP BY. To avoid this, just add ORDER BY NULL. -
使用LEFT JOIN + ORDER BY时如何避免FileSort?(How to avoid FileSort when using LEFT JOIN + ORDER BY?)[2022-07-12]
文件排序由ORDER BY引入: mysql> EXPLAIN -> SELECT * -> FROM TUser -> LEFT JOIN TMessage -> ON TUser.id=TMessage.uid -> WHERE TUser.id=3 -> ORDER BY TMessage.id DESC; +----+-------------+----------+------ ... -
如何在不使用临时文件排序的情况下按sum(:field)进行排序(How to sort by sum(:field) without using temp filesort)[2021-10-28]
如果未在其他位置使用,您可以添加另一个索引或更改现有索引。 Points列未编入索引,如果它将显着提高您的性能: CREATE INDEX user_actions_indx ON user_actions (target_type,target_id,points); You can add another index or changing the existing one if it's not used in another place . Points column is not indexe ... -
mysql索引和优化,使用where;(mysql indexes and optimizations, Using where; using temporary; using filesort)[2023-07-07]
为了避免文件,你需要一个复合索引( flow_permanent_id , entry_id ),以便MySQL可以使用WHERE和GROUP BY索引。 To avoid the filesort, you'll want a composite index on (flow_permanent_id, entry_id) so that MySQL can use the index for both the WHERE and the GROUP BY. -
你的第三个查询很好,比前两个简单得多。 但是,我不确定为什么你需要使用LEFT JOIN,也不知道为什么不使用INNER JOIN会导致文件排序。 SELECT t.id, t.name, t.permissions, t.author, t.added, (SELECT COUNT(*) FROM posts WHERE topic = t.id) as pcount, u2.username as pusername, u2.id as pauthor, p.added as padded, p.id ...
-
使用当前的执行计划(以emp开头),需要全表扫描,并且emp上没有有用的索引。 当你在join到trace_access之后只有相对较少的行时,这可能是trace_access ,这可能不是这里的情况。 为了防止文件,你需要一个支持你的order by的索引。 因此,如果尚不存在,请添加索引trace_access(Evenement_Date, Evenement_Heure) 。 这可能已经足以让MySQL以trace_access开始。 如果不是,则用STRAIGHT_JOIN emp替换INNER ...
-
我建议改变 WHERE o.order_status in (1,5,6) 对于 WHERE (o.order_status = 1 OR o.order_status = 5 OR o.order_status = 6) 不应该改变太多...但我知道在一些mysql版本中相当慢,也取决于你在两个表中的记录。 您也可以从选择句子中更改if,而不是在每行中执行if,您可以在只获取状态为0,3,5的行的位置使用它,并按发货状态添加组。 AND (o.shipping_status = 0 OR o.orde ...
-
SELECT * FROM big_table WHERE category = 'fruits' AND ( name = 'apple' OR taste = 'sweet' OR wildcard = '*' ) ORDER BY yyyymmdd DESC 并且有INDEX(catgory)或某些以category 开头的索引。 但是,如果超过约20%的表是category = 'fruits'则可能会决定忽略该索引并简单地进行 ...
-
您可以使用从属子查询删除临时表: select * from ( SELECT count(productID) AS commonProducts, s.productId, s.packageID FROM supply as s WHERE EXISTS ( select 1 from supply as innerS where innerS.productID in (2,3,4,5,6,7,8,9,10) and ...