我正在使用 Google 自定义搜索为我的网站上的内容编制索引。
当我使用 REST 客户端发出 get 请求时
https://www.googleapis.com/customsearch/v1?key=xxx&q=query&cx=xx
我在亚秒内得到回复。
但是当我尝试使用我的代码拨打电话时,需要花费六秒钟的时间。我究竟做错了什么 ?
__author__ = 'xxxx'
import urllib2
import logging
import gzip
from cfc.apikey.googleapi import get_api_key
from cfc.url.processor import set_query_parameter
from StringIO import StringIO
CX = 'xxx:xxx'
URL = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=sd&fields=kind,items(title)" % (get_api_key(), CX)
def get_results(query):
url = set_query_parameter(URL, 'q', query)
request = urllib2.Request(url)
request.add_header('Accept-encoding', 'gzip')
request.add_header('User-Agent','cfc xxxx (gzip)')
response = urllib2.urlopen(request)
if response.info().get('Content-Encoding') == 'gzip':
buf = StringIO(response.read())
f = gzip.GzipFile(fileobj=buf)
data = f.read()
return data
Run Code Online (Sandbox Code Playgroud)
我已经实现了性能提示中提到的性能提示。我将不胜感激任何帮助。谢谢。
我们有一个分析产品。我们为每位客户提供一个 JavaScript 代码,他们将其放入他们的网站中。如果用户访问我们的客户站点,Java 脚本代码就会访问我们的服务器,以便我们代表该客户存储此页面访问。每个客户都包含唯一的域名。
我们将此页面访问存储在 MySql 表中。
以下是表架构。
CREATE TABLE `page_visits` (
`domain` varchar(50) DEFAULT NULL,
`guid` varchar(100) DEFAULT NULL,
`sid` varchar(100) DEFAULT NULL,
`url` varchar(2500) DEFAULT NULL,
`ip` varchar(20) DEFAULT NULL,
`is_new` varchar(20) DEFAULT NULL,
`ref` varchar(2500) DEFAULT NULL,
`user_agent` varchar(255) DEFAULT NULL,
`stats_time` datetime DEFAULT NULL,
`country` varchar(50) DEFAULT NULL,
`region` varchar(50) DEFAULT NULL,
`city` varchar(50) DEFAULT NULL,
`city_lat_long` varchar(50) DEFAULT NULL,
`email` varchar(100) DEFAULT NULL,
KEY `sid_index` (`sid`) USING BTREE,
KEY `domain_index` (`domain`),
KEY `email_index` (`email`),
KEY …Run Code Online (Sandbox Code Playgroud) 我使用MSStorageDriver_FailurePredictThresholds, MSStorageDriver_ATAPISmartData,MSStorageDriver_FailurePredictStatus类来获取相关信息,但没有得到正确的结果。
我有两个独立的查询,它们具有相同的输出。现在我想了解哪一个更好?
查询1:
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|----|-------------|-------|------|---------------|--------|---------|--------|------|----------------------------------------------------|
| 1 | SIMPLE | t1 | ALL | (null) | (null) | (null) | (null) | 9 | Using where |
| 1 | SIMPLE | t2 | ALL | (null) | (null) | (null) | (null) | 9 | Using where; Using join buffer (Block Nested Loop) |
Run Code Online (Sandbox Code Playgroud)
查询2:
| id | select_type | …Run Code Online (Sandbox Code Playgroud) 我有一个使用多个连接的视图的 SQL 查询,该查询偶尔运行非常慢 - 比正常情况慢很多,使查询几乎无法使用。
我从视图中复制了查询并进行了试验并在https://dba.stackexchange.com/a/60180/52607找到了解决方案- 如果我添加
OPTION (MERGE JOIN, HASH JOIN)
Run Code Online (Sandbox Code Playgroud)
到查询结束时,它的运行速度提高了约 6 倍。
我现在尝试将 OPTION 调整为原始视图,但 SQL Server/SSMS 告诉我
Incorrect syntax near the keyword 'OPTION'.
Run Code Online (Sandbox Code Playgroud)
如何将此选项添加到视图中,以便视图的结果查询同样快?
(将选项添加到视图上的查询并没有导致任何加速。这看起来像这样:
select * from vMyView
where SomeDate >= CONVERT(Datetime, '2017.09.20')
OPTION (MERGE JOIN, HASH JOIN)
Run Code Online (Sandbox Code Playgroud)
我想我必须直接将此选项用于 vMyView - 如果可能的话。)
我是 SQL 新手,我有一个问题。
我有这个 SQL 代码:
DROP TABLE if exists students;
DROP TABLE if exists grades;
CREATE TABLE students(
s_id integer NOT NULL PRIMARY KEY,
s_name text,
s_last_name text,
curr_year integer
);
CREATE TABLE grades(
s_id integer NOT NULL PRIMARY KEY,
course text,
c_year integer,
grade integer,
FOREIGN KEY (s_id) REFERENCES students
);
INSERT INTO students (s_id, s_name, s_last_name, curr_year)
VALUES (1, 'A', 'S', 3);
INSERT INTO students (s_id, s_name, s_last_name, curr_year)
VALUES (2, 'A', 'A', 2);
INSERT INTO students …Run Code Online (Sandbox Code Playgroud) 集群实现的性能改进给我留下了深刻的印象,但不知道它需要多长时间。
我知道如果在集群之后更改了表或分区,则需要重新构建集群,但是除非我记下了上次对表进行集群的时间,否则我怎么知道什么时候需要再次进行集群?
我可以使用这个查询来告诉我哪些表有一个或多个聚集索引
SELECT *
FROM pg_class c
JOIN pg_index i ON i.indrelid = c.oid
WHERE relkind = 'r' AND relhasindex AND i.indisclustered
Run Code Online (Sandbox Code Playgroud)
我的问题是。
我注意到重新构建聚集索引所需的时间与最初构建它所需的时间一样长(即使在此期间没有触及表)。所以我想避免重新聚类,除非我知道表需要它。
更新为清晰(我希望)
如果我使用这个命令......
CLUSTER tableA USING tableA_idx1;
Run Code Online (Sandbox Code Playgroud)
像许多 Postgres 一样,n00bs我们有很多带有外键约束但未建立索引的表。在某些情况下,这不会对性能造成很大影响 - 但这需要进一步分析。
我已阅读以下文章:https://www.cybertec-postgresql.com/en/index-your-foreign-key/
并使用以下查询查找所有没有索引的外键:
SELECT c.conrelid::regclass AS "table",
/* list of key column names in order */
string_agg(a.attname, ',' ORDER BY x.n) AS columns,
pg_catalog.pg_size_pretty(
pg_catalog.pg_relation_size(c.conrelid)
) AS size,
c.conname AS constraint,
c.confrelid::regclass AS referenced_table
FROM pg_catalog.pg_constraint c
/* enumerated key column numbers per foreign key */
CROSS JOIN LATERAL
unnest(c.conkey) WITH ORDINALITY AS x(attnum, n)
/* name for each key column */
JOIN pg_catalog.pg_attribute a
ON a.attnum = x.attnum
AND a.attrelid = c.conrelid …Run Code Online (Sandbox Code Playgroud) 我已经找了一个星期了,恐怕这可能不存在[还]。我想在 PostgreSQL 中使用一个跨越多个表的索引。Oracle 和 SQL 服务器似乎实现了它们(或多或少的选项)。
对于我需要实现的某些搜索,它可能非常有用。
作为参考,这里是Oracle 和 SQL Server的多表索引示例:
甲骨文示例
Oracle可以创建位图连接索引,如下图:
create table dealer (
id int primary key not null,
city varchar2(20) not null
);
create table car (
id int primary key not null,
brand varchar2(20),
price int,
dealer_id int references dealer (id)
);
create bitmap index bix1 on car (d.city, c.brand)
from car c, dealer d
where d.id = c.dealer_id;
select avg(c.price)
from dealer d
join car c on c.dealer_id = …Run Code Online (Sandbox Code Playgroud) 我有一张表:student_homework,其复合索引之一是uk_sid_lsnid_version(student_id, lesson_id, curriculum_version, type):
student_homework 0 uk_sid_lsnid_version 1 student_id A 100 BTREE
student_homework 0 uk_sid_lsnid_version 2 lesson_id A 100 BTREE
student_homework 0 uk_sid_lsnid_version 3 curriculum_version A 100 BTREE
student_homework 0 uk_sid_lsnid_version 4 type A 100 BTREE
Run Code Online (Sandbox Code Playgroud)
现在我有一个 Sql:
select * from student_homework where student_id=100 and type=1结果explain如下:
1 SIMPLE student_homework ref uk_sid_lsnid_version,idx_student_id_update_time uk_sid_lsnid_version 4 const 20 10.0 Using index condition
Run Code Online (Sandbox Code Playgroud)
执行计划是uk_sid_lsnid_version。
我的问题是查询条件type在这里如何工作?数据库引擎是否扫描所有(缩小的)记录?根据我的理解,树形层次结构是:
student_id
/ \
lesson_id lesson_id
/ \
curriculum_version curriculum_version
/ …Run Code Online (Sandbox Code Playgroud) postgresql ×4
sql ×4
mysql ×3
indexing ×2
.net ×1
c# ×1
database ×1
explain ×1
hard-drive ×1
multi-table ×1
query-hints ×1
sql-server ×1
sql-view ×1
winforms ×1