# How does sql\_last\_value parameter work in jdbc input plugin?

**URL:** https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624
**Category:** Logstash
**Created:** [September 25, 2017, 3:43am UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624 "2017-09-25T03:43:13Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![amruth](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@amruth](https://discuss.elastic.co/u/amruth)
#### Post date: [September 25, 2017, 3:43am UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/1 "2017-09-25T03:43:13Z")

</div>

Hi,

I am using JDBC input plugin with

use\_column\_value =\> true  
tracking\_column =\> "id".

But the problem is that my id column is not an incremental all the time. For example it may be as 1,2,3,5,6,7,10,9,8. So what does the file for last\_run\_metadata\_path contain? 8 or 10?

Can someone please help me understand this?

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 25, 2017, 5:45am UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/2 "2017-09-25T05:45:58Z")

</div>

It'll contain 8 since it's the last value seens that's recorded. Can't you just sort on the id column to make sure the values are delivered in order?

---

<div class="post-metadata">

### Author: ![amruth](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@amruth](https://discuss.elastic.co/u/amruth)
#### Post date: [September 25, 2017, 2:55pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/3 "2017-09-25T14:55:01Z")

</div>

Hi Magnus,

Coming to the real scenario,

Total number of rows = 3294  
Last value I see in the row = 3178  
Value in last\_run\_metadat\_path file = --- 3294  
Count I see in Elasticsearch(from Kibana) = 3541

I don't quite understand the logic here. Can you please explain?

Thanks

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 25, 2017, 7:32pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/4 "2017-09-25T19:32:20Z")

</div>

> Total number of rows = 3294  
> Last value I see in the row = 3178  
> Value in last\_run\_metadat\_path file = --- 3294

And what query did you end up using?

> Count I see in Elasticsearch(from Kibana) = 3541

Well, I can't tell you what those extra 250 documents come from. Are you sure you started with an empty index? What does your elasticsearch output configuration look like?

---

<div class="post-metadata">

### Author: ![amruth](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@amruth](https://discuss.elastic.co/u/amruth)
#### Post date: [September 26, 2017, 1:53am UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/5 "2017-09-26T01:53:33Z")

</div>

> [@magnusbaeck](#):
>
> And what query did you end up using?

statement =\> "SELECT \* from table\_name WHERE id \> :sql\_last\_value"

> [@magnusbaeck](#):
>
> Are you sure you started with an empty index?

Yes, I started with an empty index.

> [@magnusbaeck](#):
>
> What does your elasticsearch output configuration look like?

```
elasticsearch {  
	hosts => " ******"
	index => "twiiter" 
}

```

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 26, 2017, 5:11am UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/6 "2017-09-26T05:11:58Z")

</div>

> statement =\> "SELECT \* from table\_name WHERE id \> :sql\_last\_value"

Where's the ORDER BY clause?

---

<div class="post-metadata">

### Author: ![amruth](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@amruth](https://discuss.elastic.co/u/amruth)
#### Post date: [September 26, 2017, 1:35pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/7 "2017-09-26T13:35:16Z")

</div>

> [@magnusbaeck](#):
>
> Where's the ORDER BY clause?

statement =\> "SELECT \* from table\_name WHERE id \> :sql\_last\_value ORDER by id"

If I use this statement, will it work correctly? I am assuming it picks the id which is greater than sql\_last\_value and then perform "ORDER by".

---

<div class="post-metadata">

### Author: ![magnusbaeck](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/magnusbaeck/32/44943_2.png) [@magnusbaeck](https://discuss.elastic.co/u/magnusbaeck)
#### Post date: [September 26, 2017, 1:41pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/8 "2017-09-26T13:41:05Z")

</div>

Yes. The highest value will then be processed last, so the next time you run the query it'll ignore all previously seen values.

---

<div class="post-metadata">

### Author: ![amruth](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@amruth](https://discuss.elastic.co/u/amruth)
#### Post date: [September 26, 2017, 3:55pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/9 "2017-09-26T15:55:30Z")

</div>

Okay, will try it. Thank you 🙂

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [October 24, 2017, 3:55pm UTC](https://discuss.elastic.co/t/how-does-sql-last-value-parameter-work-in-jdbc-input-plugin/101624/10 "2017-10-24T15:55:35Z")

</div>

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