# Duplicate query results from Virtual Graphs when using two (connected) bindings

**URL:** <https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129>\
**Category:** Support\
**Created:** [July 9, 2021, 8:54am UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129 "2021-07-09T08:54:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![teatime](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@teatime](https://community.stardog.com/u/teatime)\
**Post date:** [July 9, 2021, 8:54am UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/1 "2021-07-09T08:54:33Z")

</div>

Dear Stardog Community,

I am using a MySQL-Database with a single table and connect it as a virtual Graph to Stardog. The content of the table is:  
**id, name, location\_id**  
'1', 'Example 1', _'Location 1'_  
'2', 'Example 2', 'Location 2'  
'3', 'Example 3', 'Location 3'  
'4', 'Example 4', _'Location 1'_

As you see, the last location\_id is the same as the first one.

My Mapping creates an instance of "Example" and one of "Location" and links them with the predicate "atLocation". This is the Mapping:

> MAPPING  
> FROM SQL {  
> SELECT \*  
> FROM `example`.`example`  
> }  
> TO {  
> ?subject a :Example;  
> :id ?id ;  
> :name ?name ;  
> :atLocation ?location .
> 
> ?location a :Location;  
> :id ?location\_id .
> 
> } WHERE {  
> BIND(template("[http://api.stardog.com/Example/id={id}](http://api.stardog.com/Example/id=%7Bid%7D)") AS ?subject)  
> BIND(template("[http://api.stardog.com/Location/id={location\_id}](http://api.stardog.com/Location/id=%7Blocation_id%7D)") AS ?location)  
> }

If I run now the following select statement, it is giving me 10 results:

> SELECT ?name ?locId {  
> GRAPH virtual://example {  
> ?example a :Example ;  
> :name ?name ;  
> :atLocation ?location .
> 
> ```
> ?location a :Location ;
> :id ?locId .
> 
> ```
> 
> }  
> }  
> ORDER BY ?name

These are the results

> name,locId  
> Example 1,Location 1  
> Example 1,Location 1  
> Example 1,Location 1  
> Example 1,Location 1  
> Example 2,Location 2  
> Example 3,Location 3  
> Example 4,Location 1  
> Example 4,Location 1  
> Example 4,Location 1  
> Example 4,Location 1

If I copy the data locally using _COPY virtual://example TO [http://stardog.com/Example](http://stardog.com/Example)_ and then running the select-statement without referring to the graph, but to the local data, I get the expected results:

> name,locId  
> Example 1,Location 1  
> Example 2,Location 2  
> Example 3,Location 3  
> Example 4,Location 1

Is there something wrong with my mapping? Can you suggest what to do to get the expected results without needing to copy the data locally?

Thank you for your support!  
Tim

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [July 9, 2021, 12:42pm UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/2 "2021-07-09T12:42:33Z")

</div>

Hello, and welcome to community!

Essentially when querying a virtual graph directly, depending on the mapping and underlying data, sometimes you will see duplicate results. If you were to add `DISTINCT` to your select query, you would see the same 4 results that you're expecting.

The reason that this _doesn't_ happen after a `COPY` is that when Stardog is instructed to add a triple to a graph that already exists, it's simply a no-op. Basically the difference is that your VG mapping creates a collection of triples to query over, while adding them into Stardog itself creates a Set.

---

<div class="post-metadata">

**Author:** ![teatime](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@teatime](https://community.stardog.com/u/teatime)\
**Post date:** [July 9, 2021, 12:57pm UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/3 "2021-07-09T12:57:59Z")

</div>

Thank you so much for your help and explanation Stephen!

The problem is that there are so many triples added when working with larger tables that I run into timeouts. In my use case (that has the same structure like the one in the example), I get ~1500 results. In the local copy it finishes in less than 100ms, with the Virtual Graph it takes nearly 3 minutes. If I don't add "distinct" to the query, it returns more than 2,000,000 results, that's probably why it needs so long.

This is on the local copy:  
 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ec6d1f1759cbf4a17a11c0c6510c25bf7a59c2d4.png)

This is on the Virtual Graph:

 ![image](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/3/34df83992f1cb7f5b71ae9542e5f6393d386df20.png)

Do you have any suggestions on how I could speed that up?

Thank you!

---

<div class="post-metadata">

**Author:** ![stephen](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.stardog.com/stephen/32/27_2.png) [@stephen](https://community.stardog.com/u/stephen)\
**Post date:** [July 9, 2021, 1:10pm UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/4 "2021-07-09T13:10:46Z")

</div>

I might recommend changing the `SELECT *` in your VG mapping to something more selective, i.e., only pulling the columns that you need for the mapping. You could also generate the query plan for the virtual query to see if the issue is with the SQL query that is ultimately being generated.

---

<div class="post-metadata">

**Author:** ![teatime](https://avatars.discourse-cdn.com/v4/letter/t/f19dbf/32.png) [@teatime](https://community.stardog.com/u/teatime)\
**Post date:** [July 12, 2021, 10:12am UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/5 "2021-07-12T10:12:45Z")

</div>

Thank you so much for the suggestion Stephen!

I was going in more detail through the Query Plan and realized some problems. It was only accessing one table, but it was doing multiple joins over the same table and the same column.

So I made the following changes / improvements:

**In my MySQL-DB:**

- I didn't set up the id columns (it's a combination of 3 columns) as primary key, but just as normal columns. So I changed them to form the primary key
- I had the columns set up as text instead of VARCHAR (so Stardog always cast it to a VARCHAR(2048) which could have caused some performance problems)

**In my Stardog Mapping:**

- I set up the Bindings over all ID columns (before I only used one of the ID field as part of the URI, so there were duplicates). That's how the mapping for my associated location looks now:

> BIND(template("[http://api.stardog.com/Location/LOCATION={LOCATION};COMMAND={COMMAND};RULE\_ID={RULE\_ID}](http://api.stardog.com/Location/LOCATION=%7BLOCATION%7D;COMMAND=%7BCOMMAND%7D;RULE_ID=%7BRULE_ID%7D)") AS ?location)

I re-added Data-Source and Virtual Graphs after applying the changes on the DB. Now it is working like expected and giving me not even duplicate results. I hope that can help as reference for somebody running into similar issues.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex030/uploads/stardog/original/2X/e/ed66b48a616f505106a4acde3c9cee7e6da9bc67.svg) [@system](https://community.stardog.com/u/system)\
**Post date:** [July 26, 2021, 10:13am UTC](https://community.stardog.com/t/duplicate-query-results-from-virtual-graphs-when-using-two-connected-bindings/3129/6 "2021-07-26T10:13:41Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
