想象一下,我有一个存储一系列稀疏向量的表.稀疏向量意味着它仅在数据结构中显式存储非零值.我可以有一个100万维向量,但我只存储非零的维度的值.因此,大小与非零条目的数量成比例,而不是向量的维度.
表定义如下:vector_id:int dimension:int value:float
现在,在正常的编程范围内,我可以在O(| v1 | + | v2 |)时间内计算两个向量的内积或点积.基本上算法是存储按维度排序的稀疏向量并迭代每个维度,直到找到维度之间的碰撞并乘以共享维度的值并继续添加它们直到你到达任一个向量的末尾.
什么是在SQL中实现这一目标的最快方法?
在MySQL中获取累积总和的"正确"查询是什么?
我有一个表格,我保存有关文件的信息,一列列表包含文件的大小(以字节为单位).(实际文件保存在某处的磁盘上)
我想得到这样的累积文件大小:
+------------+---------+--------+----------------+
| fileInfoId | groupId | size | cumulativeSize |
+------------+---------+--------+----------------+
| 1 | 1 | 522120 | 522120 |
| 2 | 2 | 316042 | 316042 |
| 4 | 2 | 711084 | 1027126 |
| 5 | 2 | 697002 | 1724128 |
| 6 | 2 | 663425 | 2387553 |
| 7 | 2 | 739553 | 3127106 |
| 8 | 2 | 700938 | 3828044 |
| 9 | …Run Code Online (Sandbox Code Playgroud) 我们这里有一个简单的SQL问题.在varchar列中,我们希望在字段中的任何位置搜索字符串.实现此性能的最佳方法是什么?显然,一个指数在这里没有任何帮助,其他任何技巧?
我们使用MySQL并拥有大约300万条记录.我们需要每秒执行许多这些查询,因此我们真正尝试以最佳性能实现这些查询.
到目前为止,最简单的方法是:
Select * from table where column like '%search%'
Run Code Online (Sandbox Code Playgroud)
我应该进一步指定该列实际上是一个长字符串,如"sadfasdfwerwe",我必须在此列中搜索"asdf".所以他们不是句子而是试图匹配他们中的一个词.全文搜索仍然有用吗?
我正在使用MySQLTuner.pl来优化我的网站....虽然我不完全确定如何解决其中的一些问题,我想知道是否有人可以帮助我.
我正在使用以下MySQL设置运行16GB的RAM:
key_buffer = 1024M
max_allowed_packet = 16M
thread_stack = 192K
thread_cache_size = 8
myisam-recover = BACKUP
max_connections = 1500
table_cache = 256
thread_concurrency = 4
query_cache_limit = 2M
query_cache_size = 32M
query_cache_type = 1
tmp_table_size = 512M
max_heap_table_size = 128M
join_buffer_size = 128M
myisam_sort_buffer_size = 512M
Run Code Online (Sandbox Code Playgroud)
这是我调谐器的输出
-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.1.41-3ubuntu12.6-log
[OK] Operating on 64-bit architecture
-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB …Run Code Online (Sandbox Code Playgroud) 我有一个拥有6000万条目的数据库.
每个条目包含:
我需要选择某个月的参赛作品.每个月包含大约200万条目.
select *
from Entries
where time between "2010-04-01 00:00:00" and "2010-05-01 00:00:00"
Run Code Online (Sandbox Code Playgroud)
(查询大约需要1.5分钟)
我还想从给定的DataSourceID中选择某个月的数据.(大约需要20秒)
大约有50-100个不同的DataSourceID.
有没有办法让这更快?我有什么选择?如何优化此数据库/查询?
编辑:有约.每秒60-100次插入!
我有以下查询
SELECT translation.id
FROM "TRANSLATION" translation
INNER JOIN "UNIT" unit
ON translation.fk_id_unit = unit.id
INNER JOIN "DOCUMENT" document
ON unit.fk_id_document = document.id
WHERE document.fk_id_job = 3665
ORDER BY translation.id asc
LIMIT 50
Run Code Online (Sandbox Code Playgroud)
它运行了可怕的110秒.
表格大小:
+----------------+-------------+
| Table | Records |
+----------------+-------------+
| TRANSLATION | 6,906,679 |
| UNIT | 6,906,679 |
| DOCUMENT | 42,321 |
+----------------+-------------+
Run Code Online (Sandbox Code Playgroud)
但是,当我将LIMIT参数从50 更改为1000时,查询将在2秒内完成.
这是慢速查询计划
Limit (cost=0.00..146071.52 rows=50 width=8) (actual time=111916.180..111917.626 rows=50 loops=1)
-> Nested Loop (cost=0.00..50748166.14 rows=17371 width=8) …Run Code Online (Sandbox Code Playgroud) 这感觉就像是"为我做我的功课"这样的问题,但我真的被困在这里试图使这个查询快速运行对很多行的表.这是一个显示架构的SQLFiddle(或多或少).
我已经使用了索引,试图获得一些能够显示所有必需列但却没有取得多大成功的东西.这是create:
CREATE TABLE `AuditEvent` (
`auditEventId` bigint(20) NOT NULL AUTO_INCREMENT,
`eventTime` datetime NOT NULL,
`target1Id` int(11) DEFAULT NULL,
`target1Name` varchar(100) DEFAULT NULL,
`target2Id` int(11) DEFAULT NULL,
`target2Name` varchar(100) DEFAULT NULL,
`clientId` int(11) NOT NULL DEFAULT '1',
`type` int(11) not null,
PRIMARY KEY (`auditEventId`),
KEY `Transactions` (`clientId`,`eventTime`,`target1Id`,`type`),
KEY `TransactionsJoin` (`auditEventId`, `clientId`,`eventTime`,`target1Id`,`type`)
)
Run Code Online (Sandbox Code Playgroud)
和(的一个版本)select:
select ae.target1Id, ae.type, count(*)
from AuditEvent ae
where ae.clientId=4
and (ae.eventTime between '2011-09-01 03:00:00' and '2012-09-30 23:57:00')
group by …Run Code Online (Sandbox Code Playgroud) 我有一个基本上看起来像这样的查询:
Select *
From UserSearches us
left outer join Quotes q on q.UserSearchId = us.Id and q.QuoteNumber is not null
left outer join ContainerDetails cd on cd.QuoteId = q.Id
left outer join Surcharges s on s.ContainerDetailId = cd.Id
where us.SearchDate between @beginDate and @endDate
Run Code Online (Sandbox Code Playgroud)
给定@beginDate和@endDate的某些值,我有一个搜索需要30秒才能返回大约100K行.
最终目标是填充一些具有父子孩子关系的对象.经过一些实验,我发现我可以通过以下方式大大加快查询速度:
Select *
From UserSearches us
left outer join Quotes q on q.UserSearchId = us.Id and q.QuoteNumber is not null
left outer join ContainerDetails cd on cd.QuoteId = q.Id
where us.SearchDate between @beginDate and @endDate …Run Code Online (Sandbox Code Playgroud) 我想使用Postgres对地址进行一些基本的地理编码.我有一个地址表,有大约100万个原始地址字符串:
=> \d addresses
Table "public.addresses"
Column | Type | Modifiers
---------+------+-----------
address | text |
Run Code Online (Sandbox Code Playgroud)
我还有一张位置数据表:
=> \d locations
Table "public.locations"
Column | Type | Modifiers
------------+------+-----------
id | text |
country | text |
postalcode | text |
latitude | text |
longitude | text |
Run Code Online (Sandbox Code Playgroud)
大多数地址字符串包含邮政编码,所以我的第一次尝试是做类似和横向连接:
EXPLAIN SELECT * FROM addresses a
JOIN LATERAL (
SELECT * FROM locations
WHERE address ilike '%' || postalcode || '%'
ORDER BY LENGTH(postalcode) DESC
LIMIT 1
) AS l ON …Run Code Online (Sandbox Code Playgroud) postgresql indexing query-optimization nearest-neighbor postgresql-9.4
我有一个非常复杂的视图,形式如下
create or replace view loan_vw as
select * from (with loan_info as (select loan_table.*,commission_table.*
from loan_table,
commission_table where
contract_id=commission_id)
select /*complex transformations */ from loan_info
where type <> 'PRINCIPAL'
union all
select /*complex transformations */ from loan_info
where type = 'PRINCIPAL')
Run Code Online (Sandbox Code Playgroud)
现在,如果我执行以下操作,则查询挂起
select * from loan_vw where contract_id='HA001234TY56';
Run Code Online (Sandbox Code Playgroud)
但是,如果我在子查询重构中进行硬编码或在同一会话中使用包级变量,则查询将在第二秒返回
create or replace view loan_vw as
select * from (with loan_info as (select loan_table.*,commission_table.*
from loan_table,
commission_table where
contract_id=commission_id
and contract_id='HA001234TY56'
)
select /*complex transformations */ from loan_info
where type …Run Code Online (Sandbox Code Playgroud) sql ×7
mysql ×5
performance ×2
postgresql ×2
indexing ×1
optimization ×1
oracle ×1
sql-server ×1
subquery ×1