I have two tables with following records:
Table 1: 100 rows
Table 2: 50 Millions rows
Example:
Table 1: tb100
create table tb100
(
name varchar(50)
);
insert into tb100 values('Mak John'),('Will Smith'),('Luke W')......100 rows.
Table 2: tb50mil
create table tb50mil
(
name varchar(50)
);
insert into tb10mil values('John A Mak'),('K Smith Will'),('James Henry')......50 millions rows.
create nonclustered index nci_tb10mil_name on tb10mil(name);
Requirement: I want to match the name between two tables, if any WORD(John,Smith,Will) present in another table. For example John
present in John A Mark
.
My try: Used XML
to split column name
of the table tb100
and also adding collation
with CHARINDEX
.
;WITH splitdata AS
(
SELECT splitname
FROM
(
SELECT *,Cast('<X>' + Replace(t.name, ' ', '</X><X>') + '</X>' AS XML) AS xmlfilter
FROM tb100 t
)F1
CROSS apply
(
SELECT fdata.d.value('.', 'varchar(50)') AS splitName
FROM f1.xmlfilter.nodes('X') AS fdata(d)
) O
)
SELECT t2.name AS [Aadhar Names]
FROM tb50mil t2
INNER JOIN splitdata S
ON CHARINDEX(S.splitname collate Latin1_General_BIN,T2.name collate Latin1_General_BIN)>0
GROUP BY t2.name
Time taken to execution: 00:01:34
Rows affected: (2251429 row(s) affected)
Execution Plan: