Different runtimes using parameter vs. constant A strange thing is happening to me during execution a specific query.

I have this query: 

DECLARE @P0 as varchar(4000)
DECLARE @P1 as varchar(4000)

SET @P0 ='P0'
SET @P1 ='2015-02-21 00:00:00'

Select DISTINCT  top 10 USER_ID, MODULE_TYPE_CLASS, ADDRESS
From VIA_MESSAGE_AGGREGATOR ma
where state = @P0
and DATE_CREATED &lt;= @P1


This take a long to execute (about 23 seconds).

When I substitute the @P0 for 'AA' value the same query is executed in 3 secs.

This is strange because I have an index with the same columns (state and date_created) and when I execute with parameters the index is not used, but when I execute with the 'AA' value instead of parameter then SQL Server uses the index.

Anyone have any idea why its behaves like this? I'm using JPA to execute this queries, the code is just an example of the same behavior.
Ok, 

After further analysis and following the recommendation of  Aaron Bertrand and google "parameter sniffing" i found what is causing this its the SQL Server.

This is the "normal" behavior of the SQL Server, and the queries must be analysed and changed individually to resolve its problems.

Here is the link for the best description of what is happening to me:

Parameter Sniffing

Hope this help, as it help me.

Cheers,

Bigster