我需要一个建议。我需要跟踪 SQL Server 中的一个特定过程。我碰巧一个过程以超时结束,我需要找出发生这种情况的选择、插入或更新。我想问一下,我对 SQL 事件探查器知之甚少,所以我的问题是,它真的是解决我的问题的最佳工具吗?我可以用不同的方式看待某个程序吗?有更好的工具吗?
我正在尝试创建一个只能访问一个数据库的用户。它告诉我数据库不存在,但很明显它通过以下方式存在。
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | ICU Locale | Locale Provider | Access privileges
-----------+----------+----------+---------+---------+------------+-----------------+-----------------------
GarsDB | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc |
postgres | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc |
template0 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | C.UTF-8 | C.UTF-8 | | libc | =c/postgres +
| | | | | | | postgres=CTc/postgres
(4 rows)
postgres=# grant all privileges on database GarsDB to gars;
ERROR: database "garsdb" does not exist
postgres=# grant all privileges on database garsdb to gars;
ERROR: database "garsdb" does not exist
postgres=#
任何想法可能会发生什么?我最近才使用 Postgres。
我的 PostgreSQL 14.7 数据库中有一个表public.polygon_versions
:
CREATE TABLE public.polygon_versions (
id bigint NOT NULL DEFAULT nextval('polygon_versions_id_seq'::regclass),
entity_id bigint NOT NULL,
creation_transaction_id bigint NOT NULL,
obsolescence_transaction_id bigint,
geo_json json NOT NULL,
CONSTRAINT polygon_versions_pkey PRIMARY KEY (id),
CONSTRAINT polygon_versions_creation_transaction_id_foreign FOREIGN KEY (creation_transaction_id)
REFERENCES public.transactions (id),
CONSTRAINT polygon_versions_entity_id_foreign FOREIGN KEY (entity_id)
REFERENCES public.polygons (id),
CONSTRAINT polygon_versions_obsolescence_transaction_id_foreign FOREIGN KEY (obsolescence_transaction_id)
REFERENCES public.transactions (id)
);
示例geo_json
数据是:
{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[30.3626584543099,50.43004834913466],[30.37021155489584,50.4281893337737],[30.36969657076498,50.433110097683176],[30.369009925257167,50.433984846616404],[30.3626584543099,50.43004834913466]]]},"properties":{}}
我正在尝试查询字段与geo_json
某些坐标匹配的所有记录。例如:30.504106925781265、50.43731985018662 我试过以下 SQL 查询:
SELECT *
FROM polygon_versions
WHERE geo_json LIKE '%30.504106925781265,50.43731985018662%'
它返回一个错误:
ERROR: operator does not exist: json ~~ unknown
LINE 1: SELECT * FROM polygon_versions WHERE geo_json LIKE '%30.5041...
^
HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
SQL state: 42883
Character: 47
在研究 RVC 时,我发现了与比较运算符有关的官方文档<, >, >=, <=
,尽管很清楚:
select (1,2) < (1,3)
=true
由于文档中已经提到了公式a < c OR (a = c AND b < d)
,所以不清楚原因:
select (1,2,null) < (1,3,0)
=true
引用文档:
对于 <、<=、> 和 >= 情况,行元素从左到右比较,一旦找到不相等或空的元素对就停止。
和
例如,ROW(1,2,NULL) < ROW(1,3,0) 产生 true 而不是 null,因为不考虑第三对元素。
什么是“第三对”以及公式如何扩展/适应越来越多的列(例如select (1,2,3,4,5) < (1,3,0,8,9)
另外,为什么这会在我一开始就期望的时候select (1,2,0) >= (1,2,null);
返回。null
true
1>=1
我有以下交互(伪代码):
start transaction
select delivery for update # semaphore
select delivery_items where processed = false # get unprocessed data and process it
update delivery_items SET processed = true
commit # release semaphore
我原以为这足以锁定交付,以便没有两个人能够同时对其项目执行操作,但由于某种原因,我遇到了两个人确实设法修改的竞争条件(我delivery_items
是将相同的项目处理两次)。
我没有正确理解 ACID 吗?
在另一个用户完成事务之前,第一个选择是否应该阻止第二个选择和更新之间的提交阻止竞争条件?
所有 MySQL 设置都设置为默认值(某些内存池大小除外)。
如果这有什么不同的话,我正在使用 Doctrine。
我也在使用嵌套事务(doctrine 使用SAVEPOINT
s 实现它们)。
我对估计的可丢弃墓碑值的理解是 = (Number of columns where tombstones passed gc_grace_seconds/Number of columns)
。
但是在某些 sstablemetadata 输出中,我可以看到该值大于 1。这怎么可能呢?在特定的时间点,让我们假设所有列都变成墓碑,它通过了 gc_grace_seconds 然后该值将为 1。那么它随后如何大于 1 ......这取决于时间吗?即随着时间的推移,价值会增加吗?
我们有一个包含应用程序中对象的所有多边形(几何/地理)的表格。这是因为大多数多边形需要具有同一多边形的“几何”( RDNew ) 和“地理”( WGS84 ) 版本并且需要重复使用(大多数多边形用于多个对象)。
为了确保将正确的引用添加到对象(基于需要与该对象关联的多边形),我们首先使用查询在多边形表中查找多边形:
declare @__geometry_0 sys.geometry -- filled by a polygon value
SELECT TOP(1) [g].[Id], [g].[AangemaaktOp], [g].[OorspronkelijkeCoordinaatSysteem], [g].[RDNewGeo], [g].[RDNewOppervlakte], [g].[WGS84Geo], [g].[WGS84Oppervlakte]
FROM [GeoData] AS [g]
WHERE [g].[RDNewGeo].STEquals(@__geometry_0) = CAST(1 AS bit)
ORDER BY [g].[Id]
我们将 MS SQL Server 作为 Azure“服务”运行。我们遇到的问题是这些搜索操作每次大约需要 2 秒(此时表中有 250.000 多条多边形记录)。
所以我们想减少定位一个(一个)特定多边形所需的时间。
起初,我们注意到没有空间索引。所以我们添加了一个,看看它能加快搜索速度。
CREATE SPATIAL INDEX IX_SPATIAL_GeoData_RDNewGeo ON dbo.GeoData(RDNewGeo)
WITH( BOUNDING_BOX = ( xmin = 0.0, ymin = 300000.0, xmax = 280000.0, ymax = 625000.0) )
但是,运行上面的查询时,仍然没有使用索引(查看实际执行计划时)。
所以其次,我们修改了查询 NOT 使用语句top (1)
,现在使用空间索引(根据执行计划)。但是,现在搜索更慢了!
编辑:重新测试后,我们不再注意到速度下降,相反,(本地)性能提升很小 => 请参阅此问题的答案以获取完整解释。
关于如何加快执行搜索一个特定多边形的查询的任何想法(或者我们只是走错了路)?
附加信息:
SQL 服务器:Microsoft SQL Azure (RTM) - 12.0.2000.8
建表语句
CREATE TABLE [dbo].[GeoData](
[Id] [int] IDENTITY(1,1) NOT NULL,
[AangemaaktOp] [datetime2](7) NOT NULL,
[OorspronkelijkeCoordinaatSysteem] [smallint] NOT NULL,
[RDNewGeo] [geometry] NOT NULL,
[RDNewOppervlakte] [float] NOT NULL,
[WGS84Geo] [geography] NOT NULL,
[WGS84Oppervlakte] [float] NULL,
CONSTRAINT [PK_GeoData] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
示例多边形
POLYGON ((128864.429 449604.427, 128863.868 449605.748, 128863.48 449605.51, 128862.98 449605.33, 128862.47 449605.24, 128861.94 449605.25, 128861.43 449605.37, 128861.16 449605.49, 128860.11 449605.46, 128858.84 449605.43, 128856.44 449604.98, 128777.65 449582.27, 128692.42 449557.94, 128628.68 449540.22, 128547.49 449517.35, 128487.491 449500, 128443.71 449487.34, 128443.43 449487.36, 128443.22 449487.42, 128443.03 449487.51, 128442.85 449487.65, 128442.71 449487.81, 128442.6 449488, 128442.598 449488.006, 128433.38 449485.18, 128433.41 449484.9, 128433.39 449484.62, 128433.32 449484.35, 128433.19 449484.1, 128433.02 449483.87, 128432.81 449483.68, 128432.58 449483.54, 128427.179 449481.36, 128425.795 449480.801, 128427.467 449475.951, 128427.11 449473.74, 128427.51 449473.46, 128427.84 449473.11, 128428.12 449472.7, 128438.58 449441.28, 128438.98 449440.73, 128439.48 449440.25, 128440.07 449439.88, 128440.71 449439.62, 128441.4 449439.48, 128442.09 449439.48, 128442.79 449439.61, 128473.49 449448.58, 128474.623 449448.963, 128474.775 449448.997, 128474.931 449449.016, 128475.087 449449.018, 128475.243 449449.005, 128475.396 449448.975, 128475.546 449448.93, 128475.69 449448.87, 128475.828 449448.795, 128475.957 449448.707, 128476.076 449448.606, 128476.185 449448.494, 128476.281 449448.371, 128476.365 449448.239, 128476.434 449448.099, 128476.489 449447.952, 128476.507 449447.884, 128482.689 449449.572, 128482.612 449450.178, 128482.773 449450.367, 128483.043 449450.519, 128483.6 449450.72, 128483.72 449450.76, 128538 449466.25, 128587.57 449480.08, 128634.44 449494.08, 128655.143 449500, 128670.6 449504.42, 128708.1 449515.01, 128748.61 449526.58, 128778.5 449535.25, 128831.15 449550.08, 128866.91 449560.22, 128867.21 449560.24, 128867.51 449560.2, 128867.79 449560.09, 128868.05 449559.94, 128868.27 449559.74, 128868.45 449559.5, 128868.58 449559.229, 128875.66 449561.255, 128875.65 449561.67, 128875.72 449562.08, 128875.87 449562.47, 128876.03 449562.73, 128876.35 449563.92, 128876.42 449565.18, 128876.22 449566.43, 128865.22 449597.56, 128865.18 449597.95, 128865.21 449598.34, 128865.32 449598.71, 128865.5 449599.05, 128865.74 449599.35, 128866.03 449599.6, 128866.37 449599.79, 128866.395 449599.797, 128865.961 449600.819, 128864.429 449604.427))
我有一些简单的 SQL 来重置表中的一些值
UPDATE tableA SET field1 = 0
WHERE field2 IN (SELECT .......)
这工作正常,但我想修改它以返回所有field1
值在重置为零之前的值。这是我试过的......
UPDATE tableA SET field1 = 0
WHERE field2 IN (SELECT .......)
RETURNING field1
要注意的是,这仅在修改后返回 field1 的值。是否可以事先获得价值?不运行单独的查询?
Postgres 文档声明:
虽然指定字符串常量的标准语法通常很方便,但当所需字符串包含许多单引号或反斜杠时,可能很难理解,因为每个单引号或反斜杠都必须加倍。
我了解单引号是如何加倍的,因为这是根据第 4.1.2.1 节关于字符串常量的内容对它们进行转义的方式。但是如果反斜杠加倍,那么这肯定是不可能的:
janus=> \d todo
Table "public.todo"
Column | Type | Collation | Nullable | Default
--------+---------+-----------+----------+----------------------------------
id | integer | | not null | nextval('todo_id_seq'::regclass)
descr | text | | |
done | boolean | | |
janus=> truncate table todo;
TRUNCATE TABLE
janus=> insert into todo (descr,done) values ('\''', false);
INSERT 0 1
janus=> select length(descr), descr from todo;
length | descr
--------+-------
2 | \'
(1 row)
如您所见,两个单引号(被解释为一个单引号)之前的反斜杠没有任何作用,它是按字面插入的。那么引用中引用的“标准”语法如何与反斜杠交互?他们似乎没有任何特殊待遇,他们就像任何其他角色一样。
我有这些表:
Payments
vendor | VendorLocation | VendorName | CheckDate | Amount | TransactionID | InvoiceID
---------------------------------------------------------------------
777 | | Sony | 5/1/2020 | 1.50 | 345 | ABC555
777 | | Sony | 5/1/2020 | 1.20 | 444 | ABC555
777 | 103 | Sony Music | 5/1/2020 | 8.50 | 888 | XYZ888
777 | 105 | Sony VR | 5/1/2020 | 2.50 | 789 | XYZ999
777 | 105 | Sony VR | 5/1/2020 | 2.50 | 833 | XYZ111
Vendor Location
vendor | VendorLocation | LocationType |Address 1 | State | City
---------------------------------------------------------------------
777 | 103 | X |123 Ave | CA | Rivertown
777 | 105 | Z |666 Ave | CA | Northtown
777 | 106 | Z |888 Ave | CA | Southtown
Vendor Address
vendor | VendorLocation | Address 1 | State | City
---------------------------------------------------------------------
777 | | 1 Main St | CA | Rivertown
777 | 103 | 123 Ave | CA | Rivertown
777 | 105 | 666 Ave | CA | Northtown
我想要的输出是:
Vendor Spending
vendor | VendorLocation | LocationType | VendorName | TotalTransactions|TotalSpend| Address 1
---------------------------------------------------------------------
777 | | Z | Sony | 2 | $2.70 | 1 Main St
777 | 105 | Z | Sony VR | 2 | $5.00 | 666 Ave
但是,我得到的结果低于重复且不正确的 totalSpend 金额:
Vendor Spending
vendor | VendorLocation | LocationType | VendorName | TotalTransactions|TotalSpend| Address 1
---------------------------------------------------------------------
777 | | Z | Sony | 2 | $2.70 | 1 Main St
777 | 105 | Z | Sony VR | 2 | $7.70 | 1 Main St
This is query I have:
select distinct
ap.vendor Vendor_ID
,vl.VendorLocation
,vl.LocationType
,ap.VendorName
,count(ap.transactionID) as TotalTransactions
,sum( ap.amount) as TotalSpend
,ad.Address1
from payments ap
join VendorLocation vl
on ( (vl.vendor=ap.vendor and vl.VendorLocation=ap.VendorLocation)
or (vl.vendor=ap.vendor and vl.VendorLocation='')
or (vl.vendor=ap.vendor and ap.VendorLocation='')
and vl.LocationType in ('Z')
) join VENDORADDRESS ad
on (ad.VENDOR=ap.vendor AND ad.VendorLocation=ap.VendorLocation)
where
vl.LocationType in ('Z') and
ap.CheckDate>='4/1/2022'
group by
vl.vendor
,vl.LOCATION_CODE
,vl.LocationType
,ap.VendorName
,ad.Address1