# Retrieval by clause reference -- is it faster than retrieval by fact index

**URL:** <https://swi-prolog.discourse.group/t/retrieval-by-clause-reference-is-it-faster-than-retrieval-by-fact-index/720>\
**Category:** Help!\
**Created:** [May 24, 2019, 6:25am UTC](https://swi-prolog.discourse.group/t/retrieval-by-clause-reference-is-it-faster-than-retrieval-by-fact-index/720 "2019-05-24T06:25:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [May 24, 2019, 6:25am UTC](https://swi-prolog.discourse.group/t/retrieval-by-clause-reference-is-it-faster-than-retrieval-by-fact-index/720/1 "2019-05-24T06:25:04Z")

</div>

Hi,

I noticed that clause/3 enables retrieving a clause via reference.

**clause** (:Head, ?Body, ?Reference)

If i know a reference to an asserted fact; would a retrival with clause be a faster, more direct, lookup?

thank you,

Dan

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [May 24, 2019, 7:00am UTC](https://swi-prolog.discourse.group/t/retrieval-by-clause-reference-is-it-faster-than-retrieval-by-fact-index/720/2 "2019-05-24T07:00:32Z")

</div>

Depends on the data. Of course you do not have to find the clause, so that is faster. Binding head and body is done through decompilation though and that is probably slower than executing the code (never really measured). If you want fast navigation through Prolog terms stored on the heap you should probably look at recorda/3 and recorded/3, using the references as _pointers_.

---

<div class="post-metadata">

**Author:** ![grossdan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/grossdan/32/23_2.png) [@grossdan](https://swi-prolog.discourse.group/u/grossdan)\
**Post date:** [May 24, 2019, 7:07am UTC](https://swi-prolog.discourse.group/t/retrieval-by-clause-reference-is-it-faster-than-retrieval-by-fact-index/720/3 "2019-05-24T07:07:39Z")

</div>

Hi Jan,

Yes, that is my intent – thank you for the pointer to recorda, (no pun intended :-))

Essentially, I am now thinking to have for each element (node and link) in a graph an asserted fact (record). while also keeping around an assoc with Link Elements as key, and a pair or list with from and to references in the value part.

Dan
