需要进行连接使用列数据作为通配符进行通配

时间:2018-03-01 23:33:16

标签: sql database psql

嘿伙计们我正在尝试使用一个只包含URLS的表中的数据来查看我的主表中是否存在任何变量或使用它们

                  url                                                                                               │··························································································
---------------------------------------                                                                             │··························································································
 .0.9.40.52                                                                                                         │··························································································
 .00000000314.0000000265.00000225.0323                                                                              │··························································································
 .001916.com                                                                                                        │··························································································
 .00386.com                                                                                                         │··························································································
 .00-5dj-ar4c.club                                                                                                  │··························································································
 .007band.ru                                                                                                        │··························································································
 .007crconcert-japan.com                                                                                            │··························································································
 .007pi.com                                                                                                         │··························································································
 .00dt7myo.work                                                                                                     │··························································································
 .00dzhqbghr.com                                                                                                    │··························································································
(10 rows)

主表

 user_id | campaign_id |                                                                                                          url
                                |      send_time
---------+-------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------+---------------------
 8468677 |     1004001 | http://twitter.com/share?url=http%3A%2F%2Fatwonline.com%2Faircraft-orders-deliveries%2Fnigeria-s-arik-air-replaces-boeing-747s-787s&text=Nigeria%E2%80%99s+Arik+Air+replaces+Boeing+74
7s+with+787s&count=none         | 2017-01-28 13:01:28
 8468677 |     1003945 | http://twitter.com/share?url=http%3A%2F%2Fatwonline.com%2Fairframes%2Fairbaltic-cs300-performance-exceeding-expectations&text=AirBaltic%3A+CS300+performance+%E2%80%98exceeding+expect
ations%E2%80%99&count=none      | 2017-01-14 13:03:29
 8468677 |     1004189 | http://twitter.com/share?url=http%3A%2F%2Fatwonline.com%2Fairframes%2Famerican-again-defers-a350-deliveries-first-pushed-back-2020&text=American+again+defers+A350+deliveries%3B+first
+pushed+back+to+2020&count=none | 2017-05-02 12:02:04
 8468677 |     1004057 | http://twitter.com/share?url=http%3A%2F%2Fatwonline.com%2Fairframes%2Fatlas-has-acquired-all-20-767s-be-operated-amazon&text=Atlas+has+acquired+all+20+767s+to+be+operated+for+Amazon&
count=none                      | 2017-02-28 13:02:13
(4 rows)

我试图运行以下

select t1.user_id,t1.campaign_id,t1.url
from madison_alldb as t1
inner join madison_url as t2
ON t1.url LIKE CONCAT('%',t1.url, '%');

但是当我回到我的tmux时,它只是说杀了......我也不确定上述是否会起作用。

我的目标是只拥有一个域的用户限制,该域是我的URL表中的通配符匹配。

1 个答案:

答案 0 :(得分:1)

这种类型的查询很难优化。一个小的优化是使用exists而不是join

select ma.*
from madison_alldb ma
where exists (select 1
              from madison_url mu
              where ma.url like concat('%', mu.url, '%')
             )
limit 10;

但是,这仍然需要进行嵌套循环连接。唯一的区别是匹配时的比较较少。