CREATE TABLE t1 (id int primary key, id2 char(6) unique, id2_no2 varchar(6));
create index t1_id2_no2 on t1 (id2_no2);

INSERT INTO t1 VALUES (1,'100292',NULL),(2,'100979',NULL),(3,'101325',NULL),(6,'102221',NULL),(7,'102534',NULL),(9,'102594',NULL),(10,'102617',NULL),(11,'103003',NULL),(12,'103157',NULL),(15,'103538',NULL),(16,'103789',NULL),(17,'103912',NULL),(18,'104469',NULL),(19,'104851',NULL),(20,'105299',NULL),(21,'108945',NULL),(22,'102549','202549'),(23,'105586',NULL),(25,'105791',NULL),(26,'105967',NULL),(28,'106354',NULL),(29,'107245',NULL),(30,'107437',NULL),(31,'107478',NULL),(32,'107890',NULL),(33,'108156',NULL),(35,'109598',NULL),(36,'109695',NULL),(37,'109884',NULL),(38,'105493',NULL),(40,'109676',NULL),(41,'109504',NULL),(42,'105798',NULL),(68,'109845','209845'),(82,'102330',NULL),(88,'101234',NULL),(91,'105067',NULL),(97,'102138',NULL),(103,'107872',NULL),(106,'102763',NULL),(112,'104184',NULL),(114,'103358',NULL),(115,'101111',NULL),(117,'108755',NULL),(121,'106488',NULL),(127,'106994',NULL),(139,'105267','205267'),(144,'104117',NULL),(150,'108487',NULL),(152,'106556',NULL),(154,'102180',NULL),(157,'101642',NULL),(160,'103416',NULL),(163,'101595',NULL),(166,'105827',NULL),(169,'106367','206367'),(172,'106965',NULL),(173,'108084',NULL),(176,'106977',NULL),(179,'109414',NULL),(182,'100762',NULL),(188,'104420',NULL),(191,'106333',NULL),(200,'108286',NULL),(203,'102924',NULL),(206,'108368',NULL),(209,'104224',NULL),(210,'101552',NULL),(213,'100898',NULL),(216,'106188',NULL),(219,'100351',NULL),(222,'109342','209342'),(225,'208389',NULL),(228,'208765',NULL),(231,'206705',NULL),(234,'107326',NULL),(240,'103640',NULL),(243,'106353',NULL),(246,'109820',NULL),(255,'101207',NULL),(256,'104280',NULL),(259,'207248',NULL),(263,'107006',NULL),(268,'206642',NULL),(271,'107081',NULL),(274,'109269',NULL),(277,'103165',NULL),(280,'106300',NULL),(283,'102360',NULL),(286,'206010',NULL),(288,'101577',NULL),(291,'102581',NULL),(294,'208081',NULL),(297,'201049',NULL),(299,'205810',NULL);

CREATE TABLE t2 (
  id serial primary key,
  id2 char(6),
  page varchar(64),
  section varchar(64),
  name varchar(64),
  a varchar(5000),
  raa_id bigint ,
  is_old_value int,
  id3 varchar(25)
);
create unique index combined_index on t2 (id2,page,section,name,raa_id,is_old_value,id3);
create index t2_id3 on t2 (id3);
create index t2_ind on t2 (id2,page,section,name);
create index t2_id2 on t2 (id2);

INSERT INTO t2 VALUES (30,'102594','b','b','wnw','259400l3kbyevnhejf55',0,0,'5af94b27bb8333.96231003'),(31,'102594','b','b','vvv_ us prefix','2594',0,0,'5af94b27bb8333.96231003'),(32,'102594','b','b','pni_prefix','4724',0,0,'5af94b27bb8333.96231003');

analyze t1,t2;

CREATE VIEW v1d AS
SELECT distinct t2.id3 AS id3,
       t2.id2 AS id2
FROM (t2 JOIN t1 on((t2.id2 = t1.id2 OR t2.id2 = t1.id2_no2)  AND substr(t2.id2,1,2) = '10'))
WHERE t2.page = 'b'
 AND t2.section = 'b';


CREATE view v2 AS
SELECT t2.id3 AS id3,
       t2.id2 AS id2,
       max( substr(t2.a,3,octet_length(t2.a) - 4) ) AS c1
FROM (t2
      JOIN t1 on((t2.id2 = t1.id2  OR t2.id2 = t1.id2_no2)  AND substr(t2.id2,1,2) = '10'))
WHERE t2.page = 'b'
  AND t2.section = 'b'
GROUP BY t2.id3, t2.id2
ORDER BY t2.id3, t2.id2 ;

explain
SELECT *
FROM v1d
WHERE
EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 and c1 like '94%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%00%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%kb%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%ev%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%nh%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%ej%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%jf%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%40%')
AND EXISTS (SELECT * FROM v2 WHERE v1d.id2 = v2.id2 AND c1 like '%3k%')
;

