我在我的java应用程序中使用单例数据库连接,这是我的连接管理器类的代码:
public abstract class DatabaseManager {
//Static instance of connection, only one will ever exist
private static Connection connection = null;
private static String dbName="SNfinal";
//Returns single instance of connection
public static Connection getConnection(){
//If instance has not been created yet, create it
if(DatabaseManager.connection == null){
initConnection();
}
return DatabaseManager.connection;
}
//Gets JDBC connection instance
private static void initConnection(){
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;" +
"databaseName="+dbName+";integratedSecurity=true";
DatabaseManager.connection =
DriverManager.getConnection(connectionUrl);
}
catch (ClassNotFoundException e){
System.out.println(e.getMessage());
System.exit(0);
}
catch (SQLException e){ …Run Code Online (Sandbox Code Playgroud) 我真的很喜欢MongoDB,我在工作和家庭中都使用它,但是还没有遇到性能,复杂性或限制性问题。但是我一直在思考索引,但有一个问题我找不到合适的答案。
大规模SQL数据库的主要问题之一是查询的相对复杂性。具体来说,MySQL对大多数索引使用b树,这种查询比线性查询要花O(log(n))更好,但仍然意味着您拥有的数据越多,花费的时间就越长。
NoSQL数据库的一大吸引力在于消除/缓解了这种扩展问题,通常依赖于哈希样式索引,该索引具有O(1)查找时间,因此拥有更多数据不会降低应用程序的运行速度。这是我的问题所在:
根据官方的MongoDB文档,Mongo中的所有索引都使用b树。尽管Mongo实际上确实具有哈希索引,但据我所知,它们仍存储在b树中,与_id字段上的索引相同。在Mongo的文档中,我什至找不到任何能表明恒定时间的东西!
所以我的问题是这样的:实际上,Mongo中的所有索引(包括_id和哈希值)是否都存储在b树中?这是否意味着查询键(甚至通过_id来查询)实际上需要O(log(n))时间?
附录:值得注意的是,如果Mongo文档在示例查询中提供了一些复杂度公式,那将是一件很棒的事情。我最喜欢的示例是Redis文档。
另外:这是相关的。但是,我还添加了有关哈希索引和(更重要的是)_id索引的特定问题。
time-complexity mongodb database-performance query-performance
我有以下查询.由于其中的子查询,这会降低性能.我尝试了很多来添加Join而不是Subquery.但是徒劳无功 任何人都可以告诉我如何使用JOIN重写此查询?
update Table_1
set status = 'Status_2'
where status ='status_1' and (col_1, col_2, col_3, nvl(col_4,0), col_5) in (
select col_1, col_2, col_3, nvl(col_4,0), col_5 from Table_2 where status ='Status_0');
Run Code Online (Sandbox Code Playgroud)
请看SELECT * FROM table(DBMS_XPLAN.Display);下面的内容
Plan hash value: 1290346170
------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------
| 0 | UPDATE STATEMENT | | 1 | 376 | 456 (3)| 00:00:06 |
| 1 | UPDATE | Table_1 | | …Run Code Online (Sandbox Code Playgroud) 我有一个永远不会完成的查询(剩下运行超过24小时但仍在继续).
现在每个表中没有大量数据,所以我只能假设它是我编写的查询的效率.
SELECT DISTINCT s.supplier_id
FROM supplier_info s
INNER JOIN purchase_order_line_all po ON s.supplier_id = po.vendor_no
INNER JOIN purchase_req_line_all pr ON s.supplier_id = pr.vendor_no
INNER JOIN man_supp_invoice m ON s.supplier_id = m.IDENTITY
WHERE s.creation_date >= TRUNC(SYSDATE) - INTERVAL '6' MONTH
OR po.state NOT IN ('Closed', 'Cancelled')
OR pr.state NOT IN ('PO Created', 'Cancelled')
OR m.invoice_date >= TRUNC(SYSDATE) - INTERVAL '18' MONTH
Run Code Online (Sandbox Code Playgroud)
执行计划
Plan hash value: 2195330353
-------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| …Run Code Online (Sandbox Code Playgroud) 我刚刚学习MYSQL,我有这样的MySql子查询:
EXPLAIN EXTENDED SELECT brand_name, stars, hh_stock, hh_stock_value, sales_monthly_1, sales_monthly_2, sales_monthly_3, sold_monthly_1, sold_monthly_2,
sold_monthly_3, price_uvp, price_ecp, price_default, price_margin AS margin, vc_percent as vc, cogs, products_length, products_id, material_expenses,
MAX(price) AS products_price, SUM(total_sales) AS total_sales,
IFNULL(MAX(active_age), DATEDIFF(NOW(), products_date_added)) AS products_age, DATEDIFF(NOW(), products_date_added) AS jng_products_age,
AVG(sales_weekly) AS sales_weekly, AVG(sales_monthly) AS sales_monthly, SUM(total_sold) AS total_sold, SUM(total_returned) AS total_returned,
((SUM(total_returned)/SUM(total_sold)) * 100) AS returned_rate
FROM
(
SELECT p.products_id, jc.price, jc.price_end_customer AS price_ecp, jc.total_sales, jc.active_age, jc.sales_weekly,
jc.sales_monthly, jc.total_sold, jc.total_returned, jc.price_uvp, p.price_margin, p.vc_percent, p.material_expenses,
p.products_date_added, p.stars , …Run Code Online (Sandbox Code Playgroud) 我有一个10MB的以下结构的JSON文件(10k条目):
{
entry_1: {
description: "...",
offset: "...",
value: "...",
fields: {
field_1: {
offset: "...",
description: "...",
},
field_2: {
offset: "...",
description: "...",
}
}
},
entry_2:
...
...
...
Run Code Online (Sandbox Code Playgroud)
}
我想实现一个自动完成输入字段,该字段将在搜索多个属性时尽快从此文件中获取建议.例如,查找包含某些子字符串的所有条目名称,字段名称和描述.
方法1:
我试图将嵌套展平成一个字符串数组:
"entry_1|descrption|offset|value|field1|offset|description",
"entry_1|descrption|offset|value|field2|offset|description",
"entry2|..."
Run Code Online (Sandbox Code Playgroud)
并执行不区分大小写的部分字符串匹配,查询耗时约900ms.
方法2
我尝试了基于Xpath的JSON查询(使用defiant.js).
var snapshot = Defiant.getSnapshot(DATA);
found = JSON.search(snapshot, '//*[contains(fields, "substring")]');
Run Code Online (Sandbox Code Playgroud)
查询耗时约600毫秒(仅适用于单个属性fields).
是否有其他选项可以让我达到100毫秒?我控制文件格式,所以我可以把它变成XML或任何其他格式,唯一的要求是速度.
我有一个简单的计数查询,可以使用仅索引扫描,但在 PostgresQL 中仍然需要很长时间!
我有一个cars包含 2 列的表,type bigint并且active boolean这些列上还有一个多列索引
CREATE TABLE cars
(
id BIGSERIAL NOT NULL
CONSTRAINT cars_pkey PRIMARY KEY ,
type BIGINT NOT NULL ,
name VARCHAR(500) NOT NULL ,
active BOOLEAN DEFAULT TRUE NOT NULL,
created_at TIMESTAMP(0) WITH TIME ZONE default NOW(),
updated_at TIMESTAMP(0) WITH TIME ZONE default NOW(),
deleted_at TIMESTAMP(0) WITH TIME ZONE
);
CREATE INDEX cars_type_active_index ON cars(type, active);
Run Code Online (Sandbox Code Playgroud)
我插入了一些有 950k 条记录的测试数据,type=1 有 600k 条记录
INSERT INTO cars (type, name) …Run Code Online (Sandbox Code Playgroud) sql covering-index query-performance database-indexes postgresql-9.5
我正在从 Postgres 表中聚合数据,查询大约需要 2 秒,我想将其减少到不到一秒。
请在下面找到执行细节:
询问
select
a.search_keyword,
hll_cardinality( hll_union_agg(a.users) ):: int as user_count,
hll_cardinality( hll_union_agg(a.sessions) ):: int as session_count,
sum(a.total) as keyword_count
from
rollup_day a
where
a.created_date between '2018-09-01' and '2019-09-30'
and a.tenant_id = '62850a62-19ac-477d-9cd7-837f3d716885'
group by
a.search_keyword
order by
session_count desc
limit 100;
Run Code Online (Sandbox Code Playgroud)
表元数据
查询计划
Custom Scan (cost=0.00..0.00 rows=0 width=0) (actual time=1722.685..1722.694 rows=100 loops=1)
Task Count: 1
Tasks Shown: All
-> Task
Node: host=localhost port=5454 dbname=postgres
-> Limit (cost=64250.24..64250.49 …Run Code Online (Sandbox Code Playgroud) sql postgresql indexing query-performance postgresql-performance
我们有一个分析产品。我们为每位客户提供一个 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类来获取相关信息,但没有得到正确的结果。
sql ×5
database ×2
mysql ×2
oracle ×2
.net ×1
c# ×1
hard-drive ×1
indexing ×1
java ×1
javascript ×1
jdbc ×1
json ×1
mongodb ×1
postgresql ×1
sql-tuning ×1
winforms ×1
xml ×1