如何在一个表中删除带有WHERE子句的2个表行?

时间:2018-02-16 05:21:12

标签: mysql sql

这是我的2张桌子。我想删除所有超过5天的表(zipkin_annotationszipkin_spans)中包含trace_id的行

CREATE TABLE IF NOT EXISTS zipkin_spans (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL,
  `id` BIGINT NOT NULL,
  `name` VARCHAR(255) NOT NULL,
  `parent_id` BIGINT,
  `debug` BIT(1),
  `start_ts` BIGINT COMMENT 'Span.timestamp(): epoch micros used for endTs query and to implement TTL',
  `duration` BIGINT COMMENT 'Span.duration(): micros used for minDuration and maxDuration query'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_spans ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `id`) COMMENT 'ignore insert on duplicate';

CREATE TABLE IF NOT EXISTS zipkin_annotations (
  `trace_id_high` BIGINT NOT NULL DEFAULT 0 COMMENT 'If non zero, this means the trace uses 128 bit traceIds instead of 64 bit',
  `trace_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.trace_id',
  `span_id` BIGINT NOT NULL COMMENT 'coincides with zipkin_spans.id',
  `a_key` VARCHAR(255) NOT NULL COMMENT 'BinaryAnnotation.key or Annotation.value if type == -1',
  `a_value` BLOB COMMENT 'BinaryAnnotation.value(), which must be smaller than 64KB',
  `a_type` INT NOT NULL COMMENT 'BinaryAnnotation.type() or -1 if Annotation',
  `a_timestamp` BIGINT COMMENT 'Used to implement TTL; Annotation.timestamp or zipkin_spans.timestamp',
  `endpoint_ipv4` INT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_ipv6` BINARY(16) COMMENT 'Null when Binary/Annotation.endpoint is null, or no IPv6 address',
  `endpoint_port` SMALLINT COMMENT 'Null when Binary/Annotation.endpoint is null',
  `endpoint_service_name` VARCHAR(255) COMMENT 'Null when Binary/Annotation.endpoint is null'
) ENGINE=InnoDB ROW_FORMAT=COMPRESSED CHARACTER SET=utf8 COLLATE utf8_general_ci;

ALTER TABLE zipkin_annotations ADD UNIQUE KEY(`trace_id_high`, `trace_id`, `span_id`, `a_key`, `a_timestamp`) COMMENT 'Ignore insert on duplicate';

使用以下查询,我可以从zipkin_spans检索行数超过5天的行。

SELECT count(*) from zipkin_spans
WHERE FROM_UNIXTIME(zipkin_spans.start_ts/1000000) < DATE_SUB(NOW(), INTERVAL 5 DAY)

但是我希望zipkin_annotations中的行具有相同的traceIds。请就此提出建议(查询)。

2 个答案:

答案 0 :(得分:1)

您可以使用子查询执行此操作,如下所示:

SELECT * FROM zipkin_annotations WHERE trace_id IN (SELECT trace_id from zipkin_spans WHERE FROM_UNIXTIME(zipkin_spans.start_ts/1000000) < DATE_SUB(NOW(), INTERVAL 5 DAY))

内部查询使用您所需的条件从 zipkin_spans 获取所有 traceId ,而外部查询仅从 zipkin_annotations 获取记录那些 traceId s。

上述查询当然只会选择行,您可以将SELECT替换为DELETE来删除行。

另一个建议是,在设计架构时,您可以将Foreign key用于您的目的。有一个名为Delete on cascade的选项,可以删除 zipkin_annotations 中的所有行,其中 traceId 已从 zipkin_spans 。

答案 1 :(得分:1)

我认为你必须写两个查询才能从两个表中删除它(如果你没有&#34;删除级联&#34;对于&#34;外键&#34;)。

从&#34; zipkin_annotations&#34;删除数据表:

DELETE ZA
FROM zipkin_spans AS ZS
INNER JOIN zipkin_annotations AS ZA ON ZS.trace_id = ZA.trace_id
WHERE FROM_UNIXTIME(ZS.start_ts/1000000) < DATE_SUB(NOW(), INTERVAL 5 DAY)